diff --git a/hal/src/main/native/athena/AnalogInput.cpp b/hal/src/main/native/athena/AnalogInput.cpp index 1aa9d8ddcb..cf302a1085 100644 --- a/hal/src/main/native/athena/AnalogInput.cpp +++ b/hal/src/main/native/athena/AnalogInput.cpp @@ -7,9 +7,8 @@ #include "HAL/AnalogInput.h" -#include - #include +#include #include "AnalogInternal.h" #include "HAL/AnalogAccumulator.h" @@ -230,7 +229,7 @@ int32_t HAL_GetAnalogValue(HAL_AnalogInputHandle analogPortHandle, readSelect.Channel = port->channel; readSelect.Averaged = false; - std::lock_guard sync(analogRegisterWindowMutex); + std::lock_guard sync(analogRegisterWindowMutex); analogInputSystem->writeReadSelect(readSelect, status); analogInputSystem->strobeLatchOutput(status); return static_cast(analogInputSystem->readOutput(status)); @@ -261,7 +260,7 @@ int32_t HAL_GetAnalogAverageValue(HAL_AnalogInputHandle analogPortHandle, readSelect.Channel = port->channel; readSelect.Averaged = true; - std::lock_guard sync(analogRegisterWindowMutex); + std::lock_guard sync(analogRegisterWindowMutex); analogInputSystem->writeReadSelect(readSelect, status); analogInputSystem->strobeLatchOutput(status); return static_cast(analogInputSystem->readOutput(status)); diff --git a/hal/src/main/native/athena/AnalogInternal.cpp b/hal/src/main/native/athena/AnalogInternal.cpp index 8a81bea257..64657c5f8d 100644 --- a/hal/src/main/native/athena/AnalogInternal.cpp +++ b/hal/src/main/native/athena/AnalogInternal.cpp @@ -8,7 +8,8 @@ #include "AnalogInternal.h" #include -#include + +#include #include "HAL/AnalogInput.h" #include "HAL/ChipObject.h" @@ -16,7 +17,7 @@ namespace hal { -std::mutex analogRegisterWindowMutex; +wpi::mutex analogRegisterWindowMutex; std::unique_ptr analogInputSystem; std::unique_ptr analogOutputSystem; @@ -35,7 +36,7 @@ bool analogSampleRateSet = false; */ void initializeAnalog(int32_t* status) { if (analogSystemInitialized) return; - std::lock_guard sync(analogRegisterWindowMutex); + std::lock_guard sync(analogRegisterWindowMutex); if (analogSystemInitialized) return; analogInputSystem.reset(tAI::create(status)); analogOutputSystem.reset(tAO::create(status)); diff --git a/hal/src/main/native/athena/AnalogInternal.h b/hal/src/main/native/athena/AnalogInternal.h index af38a0226d..c9a3a90099 100644 --- a/hal/src/main/native/athena/AnalogInternal.h +++ b/hal/src/main/native/athena/AnalogInternal.h @@ -10,7 +10,8 @@ #include #include -#include + +#include #include "HAL/ChipObject.h" #include "HAL/Ports.h" @@ -27,7 +28,7 @@ static const uint32_t kAccumulatorChannels[] = {0, 1}; extern std::unique_ptr analogInputSystem; extern std::unique_ptr analogOutputSystem; -extern std::mutex analogRegisterWindowMutex; +extern wpi::mutex analogRegisterWindowMutex; extern bool analogSampleRateSet; struct AnalogPort { diff --git a/hal/src/main/native/athena/DIO.cpp b/hal/src/main/native/athena/DIO.cpp index 0e2e18aacd..9c7f92347b 100644 --- a/hal/src/main/native/athena/DIO.cpp +++ b/hal/src/main/native/athena/DIO.cpp @@ -17,7 +17,7 @@ using namespace hal; // Create a mutex to protect changes to the digital output values -static std::mutex digitalDIOMutex; +static wpi::mutex digitalDIOMutex; static LimitedHandleResource @@ -54,7 +54,7 @@ HAL_DigitalHandle HAL_InitializeDIOPort(HAL_PortHandle portHandle, port->channel = static_cast(channel); - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); tDIO::tOutputEnable outputEnable = digitalSystem->readOutputEnable(status); @@ -115,7 +115,7 @@ void HAL_FreeDIOPort(HAL_DigitalHandle dioPortHandle) { digitalChannelHandles.Free(dioPortHandle, HAL_HandleEnum::DIO); if (port == nullptr) return; int32_t status = 0; - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); if (port->channel >= kNumDigitalHeaders + kNumDigitalMXPChannels) { // Unset the SPI flag int32_t bitToUnset = 1 << remapSPIChannel(port->channel); @@ -205,7 +205,7 @@ void HAL_SetDigitalPWMDutyCycle(HAL_DigitalPWMHandle pwmGenerator, double rawDutyCycle = 256.0 * dutyCycle; if (rawDutyCycle > 255.5) rawDutyCycle = 255.5; { - std::lock_guard sync(digitalPwmMutex); + std::lock_guard sync(digitalPwmMutex); uint16_t pwmPeriodPower = digitalSystem->readPWMPeriodPower(status); if (pwmPeriodPower < 4) { // The resolution of the duty cycle drops close to the highest @@ -265,7 +265,7 @@ void HAL_SetDIO(HAL_DigitalHandle dioPortHandle, HAL_Bool value, if (value != 0) value = 1; } { - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); tDIO::tDO currentDIO = digitalSystem->readDO(status); if (port->channel >= kNumDigitalHeaders + kNumDigitalMXPChannels) { @@ -437,7 +437,7 @@ void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex, return; } - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); if (port->channel >= kNumDigitalHeaders + kNumDigitalMXPChannels) { // Channels 10-15 are SPI channels, so subtract our MXP channels digitalSystem->writeFilterSelectHdr(port->channel - kNumDigitalMXPChannels, @@ -465,7 +465,7 @@ int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) { return 0; } - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); if (port->channel >= kNumDigitalHeaders + kNumDigitalMXPChannels) { // Channels 10-15 are SPI channels, so subtract our MXP channels return digitalSystem->readFilterSelectHdr( @@ -492,7 +492,7 @@ int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) { void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) { initializeDigital(status); if (*status != 0) return; - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); digitalSystem->writeFilterPeriodHdr(filterIndex, value, status); if (*status == 0) { digitalSystem->writeFilterPeriodMXP(filterIndex, value, status); @@ -517,7 +517,7 @@ int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) { uint32_t hdrPeriod = 0; uint32_t mxpPeriod = 0; { - std::lock_guard sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); hdrPeriod = digitalSystem->readFilterPeriodHdr(filterIndex, status); if (*status == 0) { mxpPeriod = digitalSystem->readFilterPeriodMXP(filterIndex, status); diff --git a/hal/src/main/native/athena/DigitalInternal.cpp b/hal/src/main/native/athena/DigitalInternal.cpp index f04a618591..918409a657 100644 --- a/hal/src/main/native/athena/DigitalInternal.cpp +++ b/hal/src/main/native/athena/DigitalInternal.cpp @@ -8,10 +8,10 @@ #include "DigitalInternal.h" #include -#include #include #include +#include #include "ConstantsInternal.h" #include "HAL/AnalogTrigger.h" @@ -23,7 +23,7 @@ namespace hal { // Create a mutex to protect changes to the DO PWM config -std::mutex digitalPwmMutex; +wpi::mutex digitalPwmMutex; std::unique_ptr digitalSystem; std::unique_ptr relaySystem; @@ -31,7 +31,7 @@ std::unique_ptr pwmSystem; std::unique_ptr spiSystem; static std::atomic digitalSystemsInitialized{false}; -static std::mutex initializeMutex; +static wpi::mutex initializeMutex; DigitalHandleResource @@ -44,7 +44,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/src/main/native/athena/DigitalInternal.h b/hal/src/main/native/athena/DigitalInternal.h index 582ca492ea..403b1f1ea2 100644 --- a/hal/src/main/native/athena/DigitalInternal.h +++ b/hal/src/main/native/athena/DigitalInternal.h @@ -10,7 +10,8 @@ #include #include -#include + +#include #include "HAL/AnalogTrigger.h" #include "HAL/ChipObject.h" @@ -59,7 +60,7 @@ constexpr int32_t kDefaultPwmStepsDown = 1000; constexpr int32_t kPwmDisabled = 0; // Create a mutex to protect changes to the DO PWM config -extern std::mutex digitalPwmMutex; +extern wpi::mutex digitalPwmMutex; extern std::unique_ptr digitalSystem; extern std::unique_ptr relaySystem; diff --git a/hal/src/main/native/athena/FRCDriverStation.cpp b/hal/src/main/native/athena/FRCDriverStation.cpp index 819de77bb3..2f6c1bd66c 100644 --- a/hal/src/main/native/athena/FRCDriverStation.cpp +++ b/hal/src/main/native/athena/FRCDriverStation.cpp @@ -7,15 +7,15 @@ #include #include -#include #include #include #include -#include #include #include #include +#include +#include #include "HAL/DriverStation.h" @@ -27,9 +27,9 @@ struct HAL_JoystickAxesInt { int16_t axes[HAL_kMaxJoystickAxes]; }; -static std::mutex msgMutex; -static std::condition_variable newDSDataAvailableCond; -static std::mutex newDSDataAvailableMutex; +static wpi::mutex msgMutex; +static wpi::condition_variable newDSDataAvailableCond; +static wpi::mutex newDSDataAvailableMutex; static int newDSDataAvailableCounter{0}; extern "C" { @@ -40,7 +40,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]; @@ -315,7 +315,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; @@ -337,7 +337,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) { @@ -359,7 +359,7 @@ static void newDataOccur(uint32_t refNum) { // Since we could get other values, require our specific handle // to signal our threads if (refNum != refNumber) return; - std::lock_guard lock(newDSDataAvailableMutex); + std::lock_guard lock(newDSDataAvailableMutex); // Nofify all threads newDSDataAvailableCounter++; newDSDataAvailableCond.notify_all(); @@ -372,11 +372,11 @@ static void newDataOccur(uint32_t refNum) { */ void HAL_InitializeDriverStation(void) { static std::atomic_bool initialized{false}; - static std::mutex initializeMutex; + static wpi::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/src/main/native/athena/HAL.cpp b/hal/src/main/native/athena/HAL.cpp index 1bf6624252..a57a06d2c2 100644 --- a/hal/src/main/native/athena/HAL.cpp +++ b/hal/src/main/native/athena/HAL.cpp @@ -14,12 +14,12 @@ #include #include #include -#include #include #include #include #include +#include #include "HAL/ChipObject.h" #include "HAL/DriverStation.h" @@ -261,11 +261,11 @@ HAL_Bool HAL_GetBrownedOut(int32_t* status) { void HAL_BaseInitialize(int32_t* status) { static std::atomic_bool initialized{false}; - static std::mutex initializeMutex; + static wpi::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 @@ -323,11 +323,11 @@ static bool killExistingProgram(int timeout, int mode) { */ HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) { static std::atomic_bool initialized{false}; - static std::mutex initializeMutex; + static wpi::mutex initializeMutex; // Initial check, as if it's true initialization has finished if (initialized) return true; - std::lock_guard lock(initializeMutex); + std::lock_guard lock(initializeMutex); // Second check in case another thread was waiting if (initialized) return true; diff --git a/hal/src/main/native/athena/I2C.cpp b/hal/src/main/native/athena/I2C.cpp index 3e97643e75..53f2035e66 100644 --- a/hal/src/main/native/athena/I2C.cpp +++ b/hal/src/main/native/athena/I2C.cpp @@ -15,8 +15,8 @@ using namespace hal; -static std::mutex digitalI2COnBoardMutex; -static std::mutex digitalI2CMXPMutex; +static wpi::mutex digitalI2COnBoardMutex; +static wpi::mutex digitalI2CMXPMutex; static uint8_t i2COnboardObjCount = 0; static uint8_t i2CMXPObjCount = 0; @@ -42,9 +42,9 @@ void HAL_InitializeI2C(HAL_I2CPort port, int32_t* status) { return; } - std::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; + wpi::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::lock_guard sync(lock); + std::lock_guard sync(lock); if (port == 0) { i2COnboardObjCount++; if (i2COnBoardHandle > 0) return; @@ -90,10 +90,10 @@ int32_t HAL_TransactionI2C(HAL_I2CPort port, int32_t deviceAddress, } int32_t handle = port == 0 ? i2COnBoardHandle : i2CMXPHandle; - std::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; + wpi::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::lock_guard sync(lock); + std::lock_guard sync(lock); return i2clib_writeread( handle, deviceAddress, reinterpret_cast(dataToSend), static_cast(sendSize), reinterpret_cast(dataReceived), @@ -120,9 +120,9 @@ int32_t HAL_WriteI2C(HAL_I2CPort port, int32_t deviceAddress, } int32_t handle = port == 0 ? i2COnBoardHandle : i2CMXPHandle; - std::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; + wpi::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::lock_guard sync(lock); + std::lock_guard sync(lock); return i2clib_write(handle, deviceAddress, reinterpret_cast(dataToSend), sendSize); } @@ -149,9 +149,9 @@ int32_t HAL_ReadI2C(HAL_I2CPort port, int32_t deviceAddress, uint8_t* buffer, } int32_t handle = port == 0 ? i2COnBoardHandle : i2CMXPHandle; - std::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; + wpi::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::lock_guard sync(lock); + std::lock_guard sync(lock); return i2clib_read(handle, deviceAddress, reinterpret_cast(buffer), static_cast(count)); } @@ -162,9 +162,9 @@ void HAL_CloseI2C(HAL_I2CPort port) { // Set port out of range error here return; } - std::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; + wpi::mutex& lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::lock_guard sync(lock); + std::lock_guard sync(lock); if ((port == 0 ? i2COnboardObjCount-- : i2CMXPObjCount--) == 0) { int32_t handle = port == 0 ? i2COnBoardHandle : i2CMXPHandle; i2clib_close(handle); diff --git a/hal/src/main/native/athena/Interrupts.cpp b/hal/src/main/native/athena/Interrupts.cpp index 88be145c0d..3dc3c3287c 100644 --- a/hal/src/main/native/athena/Interrupts.cpp +++ b/hal/src/main/native/athena/Interrupts.cpp @@ -32,7 +32,7 @@ struct Interrupt { class InterruptThread : public wpi::SafeThread { public: void Main() { - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); while (m_active) { m_cond.wait(lock, [&] { return !m_active || m_notify; }); if (!m_active) break; diff --git a/hal/src/main/native/athena/Notifier.cpp b/hal/src/main/native/athena/Notifier.cpp index 18aa2816ea..4de559f4a0 100644 --- a/hal/src/main/native/athena/Notifier.cpp +++ b/hal/src/main/native/athena/Notifier.cpp @@ -10,9 +10,9 @@ #include #include // For std::atexit() #include -#include #include +#include #include "HAL/ChipObject.h" #include "HAL/Errors.h" @@ -25,8 +25,8 @@ using namespace hal; static const int32_t kTimerInterruptNumber = 28; -static std::mutex notifierInterruptMutex; -static std::mutex notifierMutex; +static wpi::mutex notifierInterruptMutex; +static wpi::mutex notifierMutex; static std::unique_ptr notifierAlarm; static std::unique_ptr notifierManager; static uint64_t closestTrigger = UINT64_MAX; @@ -46,7 +46,7 @@ struct Notifier { class NotifierThread : public wpi::SafeThread { public: void Main() { - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); while (m_active) { m_cond.wait(lock, [&] { return !m_active || m_notify; }); if (!m_active) break; @@ -126,7 +126,7 @@ void updateNotifierAlarmInternal(std::shared_ptr notifierPointer, } static void alarmCallback(uint32_t, void*) { - std::unique_lock sync(notifierMutex); + std::unique_lock sync(notifierMutex); int32_t status = 0; uint64_t currentTime = 0; @@ -182,7 +182,7 @@ HAL_NotifierHandle HAL_InitializeNotifierNonThreadedUnsafe( 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( @@ -193,7 +193,7 @@ HAL_NotifierHandle HAL_InitializeNotifierNonThreadedUnsafe( if (!notifierAlarm) notifierAlarm.reset(tAlarm::create(status)); } - std::lock_guard sync(notifierMutex); + std::lock_guard sync(notifierMutex); std::shared_ptr notifier = std::make_shared(); HAL_NotifierHandle handle = notifierHandles.Allocate(notifier); if (handle == HAL_kInvalidHandle) { @@ -236,7 +236,7 @@ HAL_NotifierHandle HAL_InitializeNotifier(HAL_NotifierProcessFunction process, void HAL_CleanNotifier(HAL_NotifierHandle notifierHandle, int32_t* status) { { - std::lock_guard sync(notifierMutex); + std::lock_guard sync(notifierMutex); auto notifier = notifierHandles.Get(notifierHandle); if (!notifier) return; @@ -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); @@ -282,7 +282,7 @@ void* HAL_GetNotifierParam(HAL_NotifierHandle notifierHandle, int32_t* status) { void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle, uint64_t triggerTime, int32_t* status) { - std::lock_guard sync(notifierMutex); + std::lock_guard sync(notifierMutex); auto notifier = notifierHandles.Get(notifierHandle); if (!notifier) return; @@ -290,7 +290,7 @@ void HAL_UpdateNotifierAlarm(HAL_NotifierHandle notifierHandle, } void HAL_StopNotifierAlarm(HAL_NotifierHandle notifierHandle, int32_t* status) { - std::lock_guard sync(notifierMutex); + std::lock_guard sync(notifierMutex); auto notifier = notifierHandles.Get(notifierHandle); if (!notifier) return; notifier->triggerTime = UINT64_MAX; diff --git a/hal/src/main/native/athena/Relay.cpp b/hal/src/main/native/athena/Relay.cpp index 85485cda26..688a574bfa 100644 --- a/hal/src/main/native/athena/Relay.cpp +++ b/hal/src/main/native/athena/Relay.cpp @@ -27,7 +27,7 @@ static IndexedHandleResource sync(digitalRelayMutex); + std::lock_guard sync(digitalRelayMutex); uint8_t relays = 0; if (port->fwd) { relays = relaySystem->readValue_Forward(status); diff --git a/hal/src/main/native/athena/SPI.cpp b/hal/src/main/native/athena/SPI.cpp index da8a7bbd4d..d0a5caa40d 100644 --- a/hal/src/main/native/athena/SPI.cpp +++ b/hal/src/main/native/athena/SPI.cpp @@ -9,10 +9,10 @@ #include #include -#include #include #include +#include #include "DigitalInternal.h" #include "HAL/DIO.h" @@ -32,9 +32,9 @@ static int32_t m_spiMXPHandle = 0; static constexpr int32_t kSpiMaxHandles = 5; // Indices 0-3 are for onboard CS0-CS2. Index 4 is for MXP. -static std::array spiHandleMutexes; -static std::array spiApiMutexes; -static std::array spiAccumulatorMutexes; +static std::array spiHandleMutexes; +static std::array spiApiMutexes; +static std::array spiAccumulatorMutexes; // MXP SPI does not count towards this std::atomic spiPortCount{0}; @@ -220,7 +220,7 @@ int32_t HAL_TransactionSPI(HAL_SPIPort port, uint8_t* dataToSend, return -1; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); return spilib_writeread( HAL_GetSPIHandle(port), reinterpret_cast(dataToSend), reinterpret_cast(dataReceived), static_cast(size)); @@ -241,7 +241,7 @@ int32_t HAL_WriteSPI(HAL_SPIPort port, uint8_t* dataToSend, int32_t sendSize) { return -1; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); return spilib_write(HAL_GetSPIHandle(port), reinterpret_cast(dataToSend), static_cast(sendSize)); @@ -265,7 +265,7 @@ int32_t HAL_ReadSPI(HAL_SPIPort port, uint8_t* buffer, int32_t count) { return -1; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); return spilib_read(HAL_GetSPIHandle(port), reinterpret_cast(buffer), static_cast(count)); } @@ -284,7 +284,7 @@ void HAL_CloseSPI(HAL_SPIPort port) { HAL_FreeSPIAccumulator(port, &status); { - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); spilib_close(HAL_GetSPIHandle(port)); } @@ -326,7 +326,7 @@ void HAL_SetSPISpeed(HAL_SPIPort port, int32_t speed) { return; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); spilib_setspeed(HAL_GetSPIHandle(port), speed); } @@ -346,7 +346,7 @@ void HAL_SetSPIOpts(HAL_SPIPort port, HAL_Bool msbFirst, return; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); spilib_setopts(HAL_GetSPIHandle(port), msbFirst, sampleOnTrailing, clkIdleHigh); } @@ -362,7 +362,7 @@ void HAL_SetSPIChipSelectActiveHigh(HAL_SPIPort port, int32_t* status) { return; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); if (port < 4) { spiSystem->writeChipSelectActiveHigh_Hdr( spiSystem->readChipSelectActiveHigh_Hdr(status) | (1 << port), status); @@ -382,7 +382,7 @@ void HAL_SetSPIChipSelectActiveLow(HAL_SPIPort port, int32_t* status) { return; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); if (port < 4) { spiSystem->writeChipSelectActiveHigh_Hdr( spiSystem->readChipSelectActiveHigh_Hdr(status) & ~(1 << port), status); @@ -402,7 +402,7 @@ int32_t HAL_GetSPIHandle(HAL_SPIPort port) { return 0; } - std::lock_guard sync(spiHandleMutexes[port]); + std::lock_guard sync(spiHandleMutexes[port]); switch (port) { case 0: return m_spiCS0Handle; @@ -431,7 +431,7 @@ void HAL_SetSPIHandle(HAL_SPIPort port, int32_t handle) { return; } - std::lock_guard sync(spiHandleMutexes[port]); + std::lock_guard sync(spiHandleMutexes[port]); switch (port) { case 0: m_spiCS0Handle = handle; @@ -463,7 +463,7 @@ static void spiAccumulatorProcess(uint64_t currentTime, // perform SPI transaction uint8_t resp_b[4]; { - std::lock_guard sync(spiApiMutexes[accum->port]); + std::lock_guard sync(spiApiMutexes[accum->port]); spilib_writeread(HAL_GetSPIHandle(accum->port), reinterpret_cast(accum->cmd), reinterpret_cast(resp_b), @@ -538,7 +538,7 @@ void HAL_InitSPIAccumulator(HAL_SPIPort port, int32_t period, int32_t cmd, return; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); if (!spiAccumulators[port]) spiAccumulators[port] = std::make_unique(); SPIAccumulator* accum = spiAccumulators[port].get(); @@ -584,7 +584,7 @@ void HAL_FreeSPIAccumulator(HAL_SPIPort port, int32_t* status) { return; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -604,7 +604,7 @@ void HAL_ResetSPIAccumulator(HAL_SPIPort port, int32_t* status) { return; } - std::lock_guard sync(spiApiMutexes[port]); + std::lock_guard sync(spiApiMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -631,7 +631,7 @@ void HAL_SetSPIAccumulatorCenter(HAL_SPIPort port, int32_t center, return; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -650,7 +650,7 @@ void HAL_SetSPIAccumulatorDeadband(HAL_SPIPort port, int32_t deadband, return; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -668,7 +668,7 @@ int32_t HAL_GetSPIAccumulatorLastValue(HAL_SPIPort port, int32_t* status) { return 0; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -688,7 +688,7 @@ int64_t HAL_GetSPIAccumulatorValue(HAL_SPIPort port, int32_t* status) { return 0; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -711,7 +711,7 @@ int64_t HAL_GetSPIAccumulatorCount(HAL_SPIPort port, int32_t* status) { return 0; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; @@ -754,7 +754,7 @@ void HAL_GetSPIAccumulatorOutput(HAL_SPIPort port, int64_t* value, return; } - std::lock_guard sync(spiAccumulatorMutexes[port]); + std::lock_guard sync(spiAccumulatorMutexes[port]); SPIAccumulator* accum = spiAccumulators[port].get(); if (!accum) { *status = NULL_PARAMETER; diff --git a/hal/src/main/native/athena/cpp/SerialHelper.cpp b/hal/src/main/native/athena/cpp/SerialHelper.cpp index 70e0b60853..354ac8bbd1 100644 --- a/hal/src/main/native/athena/cpp/SerialHelper.cpp +++ b/hal/src/main/native/athena/cpp/SerialHelper.cpp @@ -27,7 +27,7 @@ namespace hal { std::string SerialHelper::m_usbNames[2]{"", ""}; -std::mutex SerialHelper::m_nameMutex; +wpi::mutex SerialHelper::m_nameMutex; SerialHelper::SerialHelper() { viOpenDefaultRM(reinterpret_cast(&m_resourceHandle)); @@ -273,7 +273,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/src/main/native/include/HAL/cpp/SerialHelper.h b/hal/src/main/native/include/HAL/cpp/SerialHelper.h index 4f7a70ce09..4b8562c0d7 100644 --- a/hal/src/main/native/include/HAL/cpp/SerialHelper.h +++ b/hal/src/main/native/include/HAL/cpp/SerialHelper.h @@ -9,12 +9,12 @@ #include -#include #include #include #include #include +#include #include "HAL/SerialPort.h" @@ -46,7 +46,7 @@ class SerialHelper { int32_t m_resourceHandle; - static std::mutex m_nameMutex; + static wpi::mutex m_nameMutex; static std::string m_usbNames[2]; }; } // namespace hal diff --git a/hal/src/main/native/include/HAL/cpp/priority_mutex.h b/hal/src/main/native/include/HAL/cpp/priority_mutex.h deleted file mode 100644 index 4d9a4bf5e0..0000000000 --- a/hal/src/main/native/include/HAL/cpp/priority_mutex.h +++ /dev/null @@ -1,92 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#pragma once - -// Allows usage with std::lock_guard without including separately -#include - -#if !defined(__linux__) -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; - - constexpr priority_recursive_mutex() noexcept = default; - priority_recursive_mutex(const priority_recursive_mutex&) = delete; - priority_recursive_mutex& operator=(const priority_recursive_mutex&) = delete; - - // Lock the mutex, blocking until it's available. - void lock(); - - // Unlock the mutex. - void unlock(); - - // Tries to lock the mutex. - bool try_lock() noexcept; - - pthread_mutex_t* native_handle(); - - private: -// Do the equivalent of setting PTHREAD_PRIO_INHERIT and -// PTHREAD_MUTEX_RECURSIVE_NP. -#if __WORDSIZE == 64 - pthread_mutex_t m_mutex = { - {0, 0, 0, 0, 0x20 | PTHREAD_MUTEX_RECURSIVE_NP, 0, 0, {0, 0}}}; -#else - pthread_mutex_t m_mutex = { - {0, 0, 0, 0x20 | PTHREAD_MUTEX_RECURSIVE_NP, 0, {0}}}; -#endif -}; - -class priority_mutex { - public: - typedef pthread_mutex_t* native_handle_type; - - constexpr priority_mutex() noexcept = default; - priority_mutex(const priority_mutex&) = delete; - priority_mutex& operator=(const priority_mutex&) = delete; - - // Lock the mutex, blocking until it's available. - void lock(); - - // Unlock the mutex. - void unlock(); - - // Tries to lock the mutex. - bool try_lock() noexcept; - - pthread_mutex_t* native_handle(); - - private: -// Do the equivalent of setting PTHREAD_PRIO_INHERIT. -#if __WORDSIZE == 64 - pthread_mutex_t m_mutex = {{0, 0, 0, 0, 0x20, 0, 0, {0, 0}}}; -#else - pthread_mutex_t m_mutex = {{0, 0, 0, 0x20, 0, {0}}}; -#endif -}; -} // namespace hal - -#endif // FRC_SIMULATOR - -// For backwards compatibility -#ifndef NAMESPACED_PRIORITY -using hal::priority_mutex; // NOLINT -using hal::priority_recursive_mutex; // NOLINT -#endif diff --git a/hal/src/main/native/include/HAL/handles/DigitalHandleResource.h b/hal/src/main/native/include/HAL/handles/DigitalHandleResource.h index eff2448413..f5f7dc0751 100644 --- a/hal/src/main/native/include/HAL/handles/DigitalHandleResource.h +++ b/hal/src/main/native/include/HAL/handles/DigitalHandleResource.h @@ -11,7 +11,8 @@ #include #include -#include + +#include #include "HAL/Errors.h" #include "HAL/Types.h" @@ -48,7 +49,7 @@ class DigitalHandleResource : public HandleBase { private: std::array, size> m_structures; - std::array m_handleMutexes; + std::array m_handleMutexes; }; template @@ -59,7 +60,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; @@ -77,7 +78,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]; @@ -90,14 +91,14 @@ void DigitalHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue, m_version); 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(); } template void DigitalHandleResource::ResetHandles() { for (int i = 0; i < size; i++) { - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i].reset(); } HandleBase::ResetHandles(); diff --git a/hal/src/main/native/include/HAL/handles/IndexedClassedHandleResource.h b/hal/src/main/native/include/HAL/handles/IndexedClassedHandleResource.h index bfe03e8a68..7e91544ea3 100644 --- a/hal/src/main/native/include/HAL/handles/IndexedClassedHandleResource.h +++ b/hal/src/main/native/include/HAL/handles/IndexedClassedHandleResource.h @@ -12,6 +12,8 @@ #include #include +#include + #include "HAL/Errors.h" #include "HAL/Types.h" #include "HAL/cpp/make_unique.h" @@ -51,7 +53,7 @@ class IndexedClassedHandleResource : public HandleBase { private: std::array[], size> m_structures; - std::array m_handleMutexes; + std::array 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 +94,7 @@ IndexedClassedHandleResource::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]; @@ -106,7 +108,7 @@ void IndexedClassedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue, m_version); 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(); } @@ -115,7 +117,7 @@ template ::ResetHandles() { for (int i = 0; i < size; i++) { - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i].reset(); } HandleBase::ResetHandles(); diff --git a/hal/src/main/native/include/HAL/handles/IndexedHandleResource.h b/hal/src/main/native/include/HAL/handles/IndexedHandleResource.h index d2c1a254a3..5d9852dc21 100644 --- a/hal/src/main/native/include/HAL/handles/IndexedHandleResource.h +++ b/hal/src/main/native/include/HAL/handles/IndexedHandleResource.h @@ -11,7 +11,8 @@ #include #include -#include + +#include #include "HAL/Errors.h" #include "HAL/Types.h" @@ -49,7 +50,7 @@ class IndexedHandleResource : public HandleBase { 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; @@ -80,7 +81,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]; @@ -94,7 +95,7 @@ void IndexedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue, m_version); 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(); } @@ -102,7 +103,7 @@ template void IndexedHandleResource::ResetHandles() { for (int i = 0; i < size; i++) { - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i].reset(); } HandleBase::ResetHandles(); diff --git a/hal/src/main/native/include/HAL/handles/LimitedClassedHandleResource.h b/hal/src/main/native/include/HAL/handles/LimitedClassedHandleResource.h index e2c7eef568..513870b6f6 100644 --- a/hal/src/main/native/include/HAL/handles/LimitedClassedHandleResource.h +++ b/hal/src/main/native/include/HAL/handles/LimitedClassedHandleResource.h @@ -11,7 +11,8 @@ #include #include -#include + +#include #include "HAL/Types.h" #include "HAL/cpp/make_unique.h" @@ -48,8 +49,8 @@ class LimitedClassedHandleResource : public HandleBase { private: std::array, size> m_structures; - std::array m_handleMutexes; - std::mutex m_allocateMutex; + std::array m_handleMutexes; + wpi::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, m_version)); } @@ -81,7 +82,7 @@ LimitedClassedHandleResource::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]; @@ -95,8 +96,8 @@ void LimitedClassedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue, m_version); 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(); } @@ -105,9 +106,9 @@ template ::ResetHandles() { { - std::lock_guard lock(m_allocateMutex); + std::lock_guard lock(m_allocateMutex); for (int i = 0; i < size; i++) { - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i].reset(); } } diff --git a/hal/src/main/native/include/HAL/handles/LimitedHandleResource.h b/hal/src/main/native/include/HAL/handles/LimitedHandleResource.h index 5beee3c4fb..2f1ffab73f 100644 --- a/hal/src/main/native/include/HAL/handles/LimitedHandleResource.h +++ b/hal/src/main/native/include/HAL/handles/LimitedHandleResource.h @@ -11,7 +11,8 @@ #include #include -#include + +#include #include "HAL/Types.h" #include "HAL/cpp/make_unique.h" @@ -46,20 +47,20 @@ class LimitedHandleResource : public HandleBase { private: std::array, size> m_structures; - std::array m_handleMutexes; - std::mutex m_allocateMutex; + std::array m_handleMutexes; + wpi::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, m_version)); } @@ -76,7 +77,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]; @@ -90,8 +91,8 @@ void LimitedHandleResource::Free( int16_t index = getHandleTypedIndex(handle, enumValue, m_version); 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(); } @@ -99,9 +100,9 @@ template void LimitedHandleResource::ResetHandles() { { - std::lock_guard lock(m_allocateMutex); + std::lock_guard lock(m_allocateMutex); for (int i = 0; i < size; i++) { - std::lock_guard sync(m_handleMutexes[i]); + std::lock_guard sync(m_handleMutexes[i]); m_structures[i].reset(); } } diff --git a/hal/src/main/native/include/HAL/handles/UnlimitedHandleResource.h b/hal/src/main/native/include/HAL/handles/UnlimitedHandleResource.h index 9ea10f343d..cc5d9c45ad 100644 --- a/hal/src/main/native/include/HAL/handles/UnlimitedHandleResource.h +++ b/hal/src/main/native/include/HAL/handles/UnlimitedHandleResource.h @@ -10,9 +10,10 @@ #include #include -#include #include +#include + #include "HAL/Types.h" #include "HAL/handles/HandlesInternal.h" @@ -48,13 +49,13 @@ class UnlimitedHandleResource : public HandleBase { private: std::vector> m_structures; - std::mutex m_handleMutex; + wpi::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) { @@ -73,7 +74,7 @@ template std::shared_ptr UnlimitedHandleResource::Get(THandle handle) { int16_t index = getHandleTypedIndex(handle, enumValue, m_version); - 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]; @@ -83,7 +84,7 @@ template void UnlimitedHandleResource::Free( THandle handle) { int16_t index = getHandleTypedIndex(handle, enumValue, m_version); - 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(); } @@ -91,7 +92,7 @@ void UnlimitedHandleResource::Free( template void UnlimitedHandleResource::ResetHandles() { { - std::lock_guard lock(m_handleMutex); + std::lock_guard lock(m_handleMutex); for (size_t i = 0; i < m_structures.size(); i++) { m_structures[i].reset(); } diff --git a/hal/src/main/native/include/MockData/NotifyListenerVector.h b/hal/src/main/native/include/MockData/NotifyListenerVector.h index 59303fd7b6..5547d83766 100644 --- a/hal/src/main/native/include/MockData/NotifyListenerVector.h +++ b/hal/src/main/native/include/MockData/NotifyListenerVector.h @@ -8,7 +8,6 @@ #pragma once #include -#include #include #include diff --git a/hal/src/main/native/shared/cpp/priority_mutex.cpp b/hal/src/main/native/shared/cpp/priority_mutex.cpp deleted file mode 100644 index 0a31dc23c8..0000000000 --- a/hal/src/main/native/shared/cpp/priority_mutex.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#include "HAL/cpp/priority_mutex.h" - -#if defined(__linux__) - -using namespace hal; - -void priority_recursive_mutex::lock() { pthread_mutex_lock(&m_mutex); } - -void priority_recursive_mutex::unlock() { pthread_mutex_unlock(&m_mutex); } - -bool priority_recursive_mutex::try_lock() noexcept { - return !pthread_mutex_trylock(&m_mutex); -} - -pthread_mutex_t* priority_recursive_mutex::native_handle() { return &m_mutex; } - -void priority_mutex::lock() { pthread_mutex_lock(&m_mutex); } - -void priority_mutex::unlock() { pthread_mutex_unlock(&m_mutex); } - -bool priority_mutex::try_lock() noexcept { - return !pthread_mutex_trylock(&m_mutex); -} - -pthread_mutex_t* priority_mutex::native_handle() { return &m_mutex; } - -#endif diff --git a/hal/src/main/native/shared/handles/HandlesInternal.cpp b/hal/src/main/native/shared/handles/HandlesInternal.cpp index 7fdab2977f..029797f9e1 100644 --- a/hal/src/main/native/shared/handles/HandlesInternal.cpp +++ b/hal/src/main/native/shared/handles/HandlesInternal.cpp @@ -8,19 +8,19 @@ #include "HAL/handles/HandlesInternal.h" #include -#include #include +#include namespace hal { static llvm::SmallVector globalHandles; -static std::mutex& GetGlobalHandleMutex() { - static std::mutex globalHandleMutex; +static wpi::mutex& GetGlobalHandleMutex() { + static wpi::mutex globalHandleMutex; return globalHandleMutex; } HandleBase::HandleBase() { - std::lock_guard lock(GetGlobalHandleMutex()); + std::lock_guard lock(GetGlobalHandleMutex()); auto index = std::find(globalHandles.begin(), globalHandles.end(), this); if (index == globalHandles.end()) { globalHandles.push_back(this); @@ -30,7 +30,7 @@ HandleBase::HandleBase() { } HandleBase::~HandleBase() { - std::lock_guard lock(GetGlobalHandleMutex()); + std::lock_guard lock(GetGlobalHandleMutex()); auto index = std::find(globalHandles.begin(), globalHandles.end(), this); if (index != globalHandles.end()) { *index = nullptr; @@ -45,7 +45,7 @@ void HandleBase::ResetHandles() { } void HandleBase::ResetGlobalHandles() { - std::unique_lock lock(GetGlobalHandleMutex()); + std::unique_lock lock(GetGlobalHandleMutex()); for (auto&& i : globalHandles) { if (i != nullptr) { lock.unlock(); diff --git a/hal/src/main/native/sim/DriverStation.cpp b/hal/src/main/native/sim/DriverStation.cpp index 23abbae9c9..76d54004a7 100644 --- a/hal/src/main/native/sim/DriverStation.cpp +++ b/hal/src/main/native/sim/DriverStation.cpp @@ -11,19 +11,20 @@ #include #endif -#include #include #include #include -#include #include +#include +#include + #include "MockData/DriverStationDataInternal.h" #include "MockData/MockHooks.h" -static std::mutex msgMutex; -static std::condition_variable newDSDataAvailableCond; -static std::mutex newDSDataAvailableMutex; +static wpi::mutex msgMutex; +static wpi::condition_variable newDSDataAvailableCond; +static wpi::mutex newDSDataAvailableMutex; static int newDSDataAvailableCounter{0}; using namespace hal; @@ -35,7 +36,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]; @@ -220,7 +221,7 @@ bool HAL_IsNewControlData(void) { // worth the cycles to check. int currentCount = 0; { - std::unique_lock lock(newDSDataAvailableMutex); + std::unique_lock lock(newDSDataAvailableMutex); currentCount = newDSDataAvailableCounter; } if (lastCount == currentCount) return false; @@ -242,7 +243,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) { @@ -264,7 +265,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(); @@ -278,11 +279,11 @@ static int32_t newDataOccur(uint32_t refNum) { */ void HAL_InitializeDriverStation(void) { static std::atomic_bool initialized{false}; - static std::mutex initializeMutex; + static wpi::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/src/main/native/sim/HAL.cpp b/hal/src/main/native/sim/HAL.cpp index b69455a4ef..61e2212fd3 100644 --- a/hal/src/main/native/sim/HAL.cpp +++ b/hal/src/main/native/sim/HAL.cpp @@ -198,11 +198,11 @@ HAL_Bool HAL_GetBrownedOut(int32_t* status) { HAL_Bool HAL_Initialize(int32_t timeout, int32_t mode) { static std::atomic_bool initialized{false}; - static std::mutex initializeMutex; + static wpi::mutex initializeMutex; // Initial check, as if it's true initialization has finished if (initialized) return true; - std::lock_guard lock(initializeMutex); + std::lock_guard lock(initializeMutex); // Second check in case another thread was waiting if (initialized) return true; diff --git a/hal/src/main/native/sim/Interrupts.cpp b/hal/src/main/native/sim/Interrupts.cpp index 748ed8e444..9a62e3d47d 100644 --- a/hal/src/main/native/sim/Interrupts.cpp +++ b/hal/src/main/native/sim/Interrupts.cpp @@ -7,9 +7,10 @@ #include "HAL/Interrupts.h" -#include #include +#include + #include "AnalogInternal.h" #include "DigitalInternal.h" #include "ErrorsInternal.h" @@ -53,7 +54,7 @@ struct Interrupt { struct SynchronousWaitData { HAL_InterruptHandle interruptHandle; - std::condition_variable waitCond; + wpi::condition_variable waitCond; HAL_Bool waitPredicate; }; } // namespace @@ -191,7 +192,7 @@ static int64_t WaitForInterruptDigital(HAL_InterruptHandle handle, bool timedOut = false; - std::mutex waitMutex; + wpi::mutex waitMutex; #if defined(_MSC_VER) && _MSC_VER < 1900 auto timeoutTime = std::chrono::steady_clock::now() + @@ -203,7 +204,7 @@ static int64_t WaitForInterruptDigital(HAL_InterruptHandle handle, #endif { - std::unique_lock lock(waitMutex); + std::unique_lock lock(waitMutex); while (!data->waitPredicate) { if (data->waitCond.wait_until(lock, timeoutTime) == std::cv_status::timeout) { @@ -261,7 +262,7 @@ static int64_t WaitForInterruptAnalog(HAL_InterruptHandle handle, bool timedOut = false; - std::mutex waitMutex; + wpi::mutex waitMutex; #if defined(_MSC_VER) && _MSC_VER < 1900 auto timeoutTime = std::chrono::steady_clock::now() + @@ -273,7 +274,7 @@ static int64_t WaitForInterruptAnalog(HAL_InterruptHandle handle, #endif { - std::unique_lock lock(waitMutex); + std::unique_lock lock(waitMutex); while (!data->waitPredicate) { if (data->waitCond.wait_until(lock, timeoutTime) == std::cv_status::timeout) { diff --git a/hal/src/main/native/sim/MockData/AccelerometerData.cpp b/hal/src/main/native/sim/MockData/AccelerometerData.cpp index 0158f35485..ae12c2a6b2 100644 --- a/hal/src/main/native/sim/MockData/AccelerometerData.cpp +++ b/hal/src/main/native/sim/MockData/AccelerometerData.cpp @@ -32,7 +32,7 @@ int32_t AccelerometerData::RegisterActiveCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_activeCallbacks = RegisterCallback(m_activeCallbacks, "Active", callback, param, &newUid); } @@ -68,7 +68,7 @@ int32_t AccelerometerData::RegisterRangeCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_rangeCallbacks = RegisterCallback(m_rangeCallbacks, "Range", callback, param, &newUid); } @@ -104,7 +104,7 @@ int32_t AccelerometerData::RegisterXCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_xCallbacks = RegisterCallback(m_xCallbacks, "X", callback, param, &newUid); } @@ -140,7 +140,7 @@ int32_t AccelerometerData::RegisterYCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_yCallbacks = RegisterCallback(m_yCallbacks, "Y", callback, param, &newUid); } @@ -176,7 +176,7 @@ int32_t AccelerometerData::RegisterZCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_zCallbacks = RegisterCallback(m_zCallbacks, "Z", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/AccelerometerDataInternal.h b/hal/src/main/native/sim/MockData/AccelerometerDataInternal.h index 60f9986354..a07ca6b27e 100644 --- a/hal/src/main/native/sim/MockData/AccelerometerDataInternal.h +++ b/hal/src/main/native/sim/MockData/AccelerometerDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/AccelerometerData.h" #include "MockData/NotifyListenerVector.h" @@ -54,7 +56,7 @@ class AccelerometerData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_active{false}; std::shared_ptr m_activeCallbacks = nullptr; std::atomic m_range{ diff --git a/hal/src/main/native/sim/MockData/AnalogGyroData.cpp b/hal/src/main/native/sim/MockData/AnalogGyroData.cpp index b05db95a16..b379265725 100644 --- a/hal/src/main/native/sim/MockData/AnalogGyroData.cpp +++ b/hal/src/main/native/sim/MockData/AnalogGyroData.cpp @@ -28,7 +28,7 @@ int32_t AnalogGyroData::RegisterAngleCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_angleCallbacks = RegisterCallback(m_angleCallbacks, "Angle", callback, param, &newUid); } @@ -64,7 +64,7 @@ int32_t AnalogGyroData::RegisterRateCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_rateCallbacks = RegisterCallback(m_rateCallbacks, "Rate", callback, param, &newUid); } @@ -100,7 +100,7 @@ int32_t AnalogGyroData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/AnalogGyroDataInternal.h b/hal/src/main/native/sim/MockData/AnalogGyroDataInternal.h index 9d8138431d..7838c0b21f 100644 --- a/hal/src/main/native/sim/MockData/AnalogGyroDataInternal.h +++ b/hal/src/main/native/sim/MockData/AnalogGyroDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/AnalogGyroData.h" #include "MockData/NotifyListenerVector.h" @@ -40,7 +42,7 @@ class AnalogGyroData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_angle{0.0}; std::shared_ptr m_angleCallbacks = nullptr; std::atomic m_rate{0.0}; diff --git a/hal/src/main/native/sim/MockData/AnalogInData.cpp b/hal/src/main/native/sim/MockData/AnalogInData.cpp index 7226371f88..46f895ceea 100644 --- a/hal/src/main/native/sim/MockData/AnalogInData.cpp +++ b/hal/src/main/native/sim/MockData/AnalogInData.cpp @@ -40,7 +40,7 @@ int32_t AnalogInData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -76,7 +76,7 @@ int32_t AnalogInData::RegisterAverageBitsCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_averageBitsCallbacks = RegisterCallback( m_averageBitsCallbacks, "AverageBits", callback, param, &newUid); } @@ -111,7 +111,7 @@ int32_t AnalogInData::RegisterOversampleBitsCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_oversampleBitsCallbacks = RegisterCallback( m_oversampleBitsCallbacks, "OversampleBits", callback, param, &newUid); } @@ -147,7 +147,7 @@ int32_t AnalogInData::RegisterVoltageCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_voltageCallbacks = RegisterCallback(m_voltageCallbacks, "Voltage", callback, param, &newUid); } @@ -182,7 +182,7 @@ int32_t AnalogInData::RegisterAccumulatorInitializedCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_accumulatorInitializedCallbacks = RegisterCallback(m_accumulatorInitializedCallbacks, "AccumulatorInitialized", callback, param, &newUid); @@ -222,7 +222,7 @@ int32_t AnalogInData::RegisterAccumulatorValueCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_accumulatorValueCallbacks = RegisterCallback(m_accumulatorValueCallbacks, "AccumulatorValue", callback, param, &newUid); @@ -259,7 +259,7 @@ int32_t AnalogInData::RegisterAccumulatorCountCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_accumulatorCountCallbacks = RegisterCallback(m_accumulatorCountCallbacks, "AccumulatorCount", callback, param, &newUid); @@ -296,7 +296,7 @@ int32_t AnalogInData::RegisterAccumulatorCenterCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_accumulatorCenterCallbacks = RegisterCallback(m_accumulatorCenterCallbacks, "AccumulatorCenter", callback, param, &newUid); @@ -333,7 +333,7 @@ int32_t AnalogInData::RegisterAccumulatorDeadbandCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_accumulatorDeadbandCallbacks = RegisterCallback(m_accumulatorDeadbandCallbacks, "AccumulatorDeadband", callback, param, &newUid); diff --git a/hal/src/main/native/sim/MockData/AnalogInDataInternal.h b/hal/src/main/native/sim/MockData/AnalogInDataInternal.h index 0217b2ee1c..239f0e994f 100644 --- a/hal/src/main/native/sim/MockData/AnalogInDataInternal.h +++ b/hal/src/main/native/sim/MockData/AnalogInDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/AnalogInData.h" #include "MockData/NotifyListenerVector.h" @@ -85,7 +87,7 @@ class AnalogInData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_averageBits{7}; diff --git a/hal/src/main/native/sim/MockData/AnalogOutData.cpp b/hal/src/main/native/sim/MockData/AnalogOutData.cpp index b39117c600..24c140c9ee 100644 --- a/hal/src/main/native/sim/MockData/AnalogOutData.cpp +++ b/hal/src/main/native/sim/MockData/AnalogOutData.cpp @@ -26,7 +26,7 @@ int32_t AnalogOutData::RegisterVoltageCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_voltageCallbacks = RegisterCallback(m_voltageCallbacks, "Voltage", callback, param, &newUid); } @@ -62,7 +62,7 @@ int32_t AnalogOutData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/AnalogOutDataInternal.h b/hal/src/main/native/sim/MockData/AnalogOutDataInternal.h index 78ca8033cb..668d2aa7bc 100644 --- a/hal/src/main/native/sim/MockData/AnalogOutDataInternal.h +++ b/hal/src/main/native/sim/MockData/AnalogOutDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/AnalogOutData.h" #include "MockData/NotifyListenerVector.h" @@ -33,7 +35,7 @@ class AnalogOutData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_voltage{0.0}; std::shared_ptr m_voltageCallbacks = nullptr; std::atomic m_initialized{0}; diff --git a/hal/src/main/native/sim/MockData/AnalogTriggerData.cpp b/hal/src/main/native/sim/MockData/AnalogTriggerData.cpp index ed6d88921a..53889fd449 100644 --- a/hal/src/main/native/sim/MockData/AnalogTriggerData.cpp +++ b/hal/src/main/native/sim/MockData/AnalogTriggerData.cpp @@ -29,7 +29,7 @@ int32_t AnalogTriggerData::RegisterInitializedCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -64,7 +64,7 @@ int32_t AnalogTriggerData::RegisterTriggerLowerBoundCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_triggerLowerBoundCallbacks = RegisterCallback(m_triggerLowerBoundCallbacks, "TriggerLowerBound", callback, param, &newUid); @@ -101,7 +101,7 @@ int32_t AnalogTriggerData::RegisterTriggerUpperBoundCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_triggerUpperBoundCallbacks = RegisterCallback(m_triggerUpperBoundCallbacks, "TriggerUpperBound", callback, param, &newUid); @@ -138,7 +138,7 @@ int32_t AnalogTriggerData::RegisterTriggerModeCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_triggerModeCallbacks = RegisterCallback( m_triggerModeCallbacks, "TriggerMode", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/AnalogTriggerDataInternal.h b/hal/src/main/native/sim/MockData/AnalogTriggerDataInternal.h index ec5ede49a9..b32e649ce0 100644 --- a/hal/src/main/native/sim/MockData/AnalogTriggerDataInternal.h +++ b/hal/src/main/native/sim/MockData/AnalogTriggerDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/AnalogTriggerData.h" #include "MockData/NotifyListenerVector.h" @@ -49,7 +51,7 @@ class AnalogTriggerData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{0}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_triggerLowerBound{0}; diff --git a/hal/src/main/native/sim/MockData/DIOData.cpp b/hal/src/main/native/sim/MockData/DIOData.cpp index 4bdd67a4d4..80ca7f27da 100644 --- a/hal/src/main/native/sim/MockData/DIOData.cpp +++ b/hal/src/main/native/sim/MockData/DIOData.cpp @@ -32,7 +32,7 @@ int32_t DIOData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -67,7 +67,7 @@ int32_t DIOData::RegisterValueCallback(HAL_NotifyCallback callback, void* param, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_valueCallbacks = RegisterCallback(m_valueCallbacks, "Value", callback, param, &newUid); } @@ -103,7 +103,7 @@ int32_t DIOData::RegisterPulseLengthCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_pulseLengthCallbacks = RegisterCallback( m_pulseLengthCallbacks, "PulseLength", callback, param, &newUid); } @@ -138,7 +138,7 @@ int32_t DIOData::RegisterIsInputCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_isInputCallbacks = RegisterCallback(m_isInputCallbacks, "IsInput", callback, param, &newUid); } @@ -174,7 +174,7 @@ int32_t DIOData::RegisterFilterIndexCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_filterIndexCallbacks = RegisterCallback( m_filterIndexCallbacks, "FilterIndex", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/DIODataInternal.h b/hal/src/main/native/sim/MockData/DIODataInternal.h index 0105fb19e6..08e6213ce9 100644 --- a/hal/src/main/native/sim/MockData/DIODataInternal.h +++ b/hal/src/main/native/sim/MockData/DIODataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/DIOData.h" #include "MockData/NotifyListenerVector.h" @@ -54,7 +56,7 @@ class DIOData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_value{true}; diff --git a/hal/src/main/native/sim/MockData/DigitalPWMData.cpp b/hal/src/main/native/sim/MockData/DigitalPWMData.cpp index db0dc77bd9..ba1c98a531 100644 --- a/hal/src/main/native/sim/MockData/DigitalPWMData.cpp +++ b/hal/src/main/native/sim/MockData/DigitalPWMData.cpp @@ -28,7 +28,7 @@ int32_t DigitalPWMData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -64,7 +64,7 @@ int32_t DigitalPWMData::RegisterDutyCycleCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_dutyCycleCallbacks = RegisterCallback(m_dutyCycleCallbacks, "DutyCycle", callback, param, &newUid); } @@ -100,7 +100,7 @@ int32_t DigitalPWMData::RegisterPinCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_pinCallbacks = RegisterCallback(m_pinCallbacks, "Pin", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/DigitalPWMDataInternal.h b/hal/src/main/native/sim/MockData/DigitalPWMDataInternal.h index 572258428f..a251f30cc4 100644 --- a/hal/src/main/native/sim/MockData/DigitalPWMDataInternal.h +++ b/hal/src/main/native/sim/MockData/DigitalPWMDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/DigitalPWMData.h" #include "MockData/NotifyListenerVector.h" @@ -40,7 +42,7 @@ class DigitalPWMData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_dutyCycle{false}; diff --git a/hal/src/main/native/sim/MockData/DriverStationData.cpp b/hal/src/main/native/sim/MockData/DriverStationData.cpp index 43c22b1caa..d3e5f0f533 100644 --- a/hal/src/main/native/sim/MockData/DriverStationData.cpp +++ b/hal/src/main/native/sim/MockData/DriverStationData.cpp @@ -49,7 +49,7 @@ void DriverStationData::ResetData() { m_dsAttachedCallbacks = nullptr; { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickAxes = std::make_unique(6); m_joystickPOVs = std::make_unique(6); m_joystickButtons = std::make_unique(6); @@ -66,7 +66,7 @@ void DriverStationData::ResetData() { } } { - std::lock_guard lock(m_matchInfoMutex); + std::lock_guard lock(m_matchInfoMutex); m_matchInfo = std::make_unique(); } @@ -79,7 +79,7 @@ int32_t DriverStationData::RegisterEnabledCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_enabledCallbacks = RegisterCallback(m_enabledCallbacks, "Enabled", callback, param, &newUid); } @@ -114,7 +114,7 @@ int32_t DriverStationData::RegisterAutonomousCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_autonomousCallbacks = RegisterCallback( m_autonomousCallbacks, "Autonomous", callback, param, &newUid); } @@ -150,7 +150,7 @@ int32_t DriverStationData::RegisterTestCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_testCallbacks = RegisterCallback(m_testCallbacks, "Test", callback, param, &newUid); } @@ -186,7 +186,7 @@ int32_t DriverStationData::RegisterEStopCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_eStopCallbacks = RegisterCallback(m_eStopCallbacks, "EStop", callback, param, &newUid); } @@ -221,7 +221,7 @@ int32_t DriverStationData::RegisterFmsAttachedCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_fmsAttachedCallbacks = RegisterCallback( m_fmsAttachedCallbacks, "FmsAttached", callback, param, &newUid); } @@ -256,7 +256,7 @@ int32_t DriverStationData::RegisterDsAttachedCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_dsAttachedCallbacks = RegisterCallback( m_dsAttachedCallbacks, "DsAttached", callback, param, &newUid); } @@ -291,7 +291,7 @@ int32_t DriverStationData::RegisterAllianceStationIdCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_allianceStationIdCallbacks = RegisterCallback(m_allianceStationIdCallbacks, "AllianceStationId", callback, param, &newUid); @@ -332,7 +332,7 @@ int32_t DriverStationData::RegisterMatchTimeCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_matchTimeCallbacks = RegisterCallback(m_matchTimeCallbacks, "MatchTime", callback, param, &newUid); } @@ -363,22 +363,22 @@ void DriverStationData::SetMatchTime(double matchTime) { void DriverStationData::GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); *axes = m_joystickAxes[joystickNum]; } void DriverStationData::GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); *povs = m_joystickPOVs[joystickNum]; } void DriverStationData::GetJoystickButtons(int32_t joystickNum, HAL_JoystickButtons* buttons) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); *buttons = m_joystickButtons[joystickNum]; } void DriverStationData::GetJoystickDescriptor( int32_t joystickNum, HAL_JoystickDescriptor* descriptor) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); *descriptor = m_joystickDescriptor[joystickNum]; // Always ensure name is null terminated descriptor->name[255] = '\0'; @@ -387,13 +387,13 @@ void DriverStationData::GetJoystickOutputs(int32_t joystickNum, int64_t* outputs, int32_t* leftRumble, int32_t* rightRumble) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); *leftRumble = m_joystickOutputs[joystickNum].leftRumble; *outputs = m_joystickOutputs[joystickNum].outputs; *rightRumble = m_joystickOutputs[joystickNum].rightRumble; } void DriverStationData::GetMatchInfo(HAL_MatchInfo* info) { - std::lock_guard lock(m_matchInfoMutex); + std::lock_guard lock(m_matchInfoMutex); auto eventLen = m_matchInfo->eventName.size(); info->eventName = static_cast(std::malloc(eventLen + 1)); std::memcpy(info->eventName, m_matchInfo->eventName.c_str(), eventLen); @@ -414,37 +414,37 @@ void DriverStationData::FreeMatchInfo(const HAL_MatchInfo* info) { void DriverStationData::SetJoystickAxes(int32_t joystickNum, const HAL_JoystickAxes* axes) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickAxes[joystickNum] = *axes; } void DriverStationData::SetJoystickPOVs(int32_t joystickNum, const HAL_JoystickPOVs* povs) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickPOVs[joystickNum] = *povs; } void DriverStationData::SetJoystickButtons(int32_t joystickNum, const HAL_JoystickButtons* buttons) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickButtons[joystickNum] = *buttons; } void DriverStationData::SetJoystickDescriptor( int32_t joystickNum, const HAL_JoystickDescriptor* descriptor) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickDescriptor[joystickNum] = *descriptor; } void DriverStationData::SetJoystickOutputs(int32_t joystickNum, int64_t outputs, int32_t leftRumble, int32_t rightRumble) { - std::lock_guard lock(m_joystickDataMutex); + std::lock_guard lock(m_joystickDataMutex); m_joystickOutputs[joystickNum].leftRumble = leftRumble; m_joystickOutputs[joystickNum].outputs = outputs; m_joystickOutputs[joystickNum].rightRumble = rightRumble; } void DriverStationData::SetMatchInfo(const HAL_MatchInfo* info) { - std::lock_guard lock(m_matchInfoMutex); + std::lock_guard lock(m_matchInfoMutex); m_matchInfo->eventName = info->eventName; m_matchInfo->gameSpecificMessage = info->gameSpecificMessage; m_matchInfo->matchNumber = info->matchNumber; diff --git a/hal/src/main/native/sim/MockData/DriverStationDataInternal.h b/hal/src/main/native/sim/MockData/DriverStationDataInternal.h index b79ebc96f9..9f4f3cd4cd 100644 --- a/hal/src/main/native/sim/MockData/DriverStationDataInternal.h +++ b/hal/src/main/native/sim/MockData/DriverStationDataInternal.h @@ -11,6 +11,8 @@ #include #include +#include + #include "MockData/DriverStationData.h" #include "MockData/NotifyListenerVector.h" @@ -103,7 +105,7 @@ class DriverStationData { void NotifyNewData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_enabled{false}; std::shared_ptr m_enabledCallbacks = nullptr; std::atomic m_autonomous{false}; @@ -122,8 +124,8 @@ class DriverStationData { std::atomic m_matchTime{0.0}; std::shared_ptr m_matchTimeCallbacks = nullptr; - std::mutex m_joystickDataMutex; - std::mutex m_matchInfoMutex; + wpi::mutex m_joystickDataMutex; + wpi::mutex m_matchInfoMutex; std::unique_ptr m_joystickAxes; std::unique_ptr m_joystickPOVs; diff --git a/hal/src/main/native/sim/MockData/EncoderData.cpp b/hal/src/main/native/sim/MockData/EncoderData.cpp index 72842590d1..383e976bbd 100644 --- a/hal/src/main/native/sim/MockData/EncoderData.cpp +++ b/hal/src/main/native/sim/MockData/EncoderData.cpp @@ -38,7 +38,7 @@ int32_t EncoderData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -74,7 +74,7 @@ int32_t EncoderData::RegisterCountCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_countCallbacks = RegisterCallback(m_countCallbacks, "Count", callback, param, &newUid); } @@ -110,7 +110,7 @@ int32_t EncoderData::RegisterPeriodCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_periodCallbacks = RegisterCallback(m_periodCallbacks, "Period", callback, param, &newUid); } @@ -146,7 +146,7 @@ int32_t EncoderData::RegisterResetCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_resetCallbacks = RegisterCallback(m_resetCallbacks, "Reset", callback, param, &newUid); } @@ -182,7 +182,7 @@ int32_t EncoderData::RegisterMaxPeriodCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_maxPeriodCallbacks = RegisterCallback(m_maxPeriodCallbacks, "MaxPeriod", callback, param, &newUid); } @@ -218,7 +218,7 @@ int32_t EncoderData::RegisterDirectionCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_directionCallbacks = RegisterCallback(m_directionCallbacks, "Direction", callback, param, &newUid); } @@ -253,7 +253,7 @@ int32_t EncoderData::RegisterReverseDirectionCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_reverseDirectionCallbacks = RegisterCallback(m_reverseDirectionCallbacks, "ReverseDirection", callback, param, &newUid); @@ -290,7 +290,7 @@ int32_t EncoderData::RegisterSamplesToAverageCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_samplesToAverageCallbacks = RegisterCallback(m_samplesToAverageCallbacks, "SamplesToAverage", callback, param, &newUid); diff --git a/hal/src/main/native/sim/MockData/EncoderDataInternal.h b/hal/src/main/native/sim/MockData/EncoderDataInternal.h index b0c2203059..6ba70fe156 100644 --- a/hal/src/main/native/sim/MockData/EncoderDataInternal.h +++ b/hal/src/main/native/sim/MockData/EncoderDataInternal.h @@ -11,6 +11,8 @@ #include #include +#include + #include "MockData/EncoderData.h" #include "MockData/NotifyListenerVector.h" @@ -76,7 +78,7 @@ class EncoderData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_count{0}; diff --git a/hal/src/main/native/sim/MockData/I2CData.cpp b/hal/src/main/native/sim/MockData/I2CData.cpp index 338acf800f..f5d2c95828 100644 --- a/hal/src/main/native/sim/MockData/I2CData.cpp +++ b/hal/src/main/native/sim/MockData/I2CData.cpp @@ -34,7 +34,7 @@ int32_t I2CData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -69,7 +69,7 @@ int32_t I2CData::RegisterReadCallback(HAL_BufferCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_readCallbacks = RegisterCallback(m_readCallbacks, "Read", callback, param, &newUid); } @@ -86,7 +86,7 @@ int32_t I2CData::RegisterWriteCallback(HAL_BufferCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_writeCallbacks = RegisterCallback(m_writeCallbacks, "Write", callback, param, &newUid); } @@ -99,11 +99,11 @@ void I2CData::CancelWriteCallback(int32_t uid) { void I2CData::Write(int32_t deviceAddress, uint8_t* dataToSend, int32_t sendSize) { - std::lock_guard lock(m_dataMutex); + std::lock_guard lock(m_dataMutex); InvokeCallback(m_writeCallbacks, "Write", dataToSend, sendSize); } void I2CData::Read(int32_t deviceAddress, uint8_t* buffer, int32_t count) { - std::lock_guard lock(m_dataMutex); + std::lock_guard lock(m_dataMutex); InvokeCallback(m_readCallbacks, "Read", buffer, count); } diff --git a/hal/src/main/native/sim/MockData/I2CDataInternal.h b/hal/src/main/native/sim/MockData/I2CDataInternal.h index 2c9550435a..2232e7271e 100644 --- a/hal/src/main/native/sim/MockData/I2CDataInternal.h +++ b/hal/src/main/native/sim/MockData/I2CDataInternal.h @@ -11,6 +11,8 @@ #include #include +#include + #include "MockData/I2CData.h" #include "MockData/NotifyListenerVector.h" @@ -39,8 +41,8 @@ class I2CData { void ResetData(); private: - std::mutex m_registerMutex; - std::mutex m_dataMutex; + wpi::mutex m_registerMutex; + wpi::mutex m_dataMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::shared_ptr m_readCallbacks = nullptr; diff --git a/hal/src/main/native/sim/MockData/PCMData.cpp b/hal/src/main/native/sim/MockData/PCMData.cpp index e634619e80..da93555628 100644 --- a/hal/src/main/native/sim/MockData/PCMData.cpp +++ b/hal/src/main/native/sim/MockData/PCMData.cpp @@ -38,7 +38,7 @@ int32_t PCMData::RegisterSolenoidInitializedCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_solenoidInitializedCallbacks[channel] = RegisterCallback(m_solenoidInitializedCallbacks[channel], "SolenoidInitialized", callback, param, &newUid); @@ -84,7 +84,7 @@ int32_t PCMData::RegisterSolenoidOutputCallback(int32_t channel, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_solenoidOutputCallbacks[channel] = RegisterCallback(m_solenoidOutputCallbacks[channel], "SolenoidOutput", callback, param, &newUid); @@ -123,7 +123,7 @@ int32_t PCMData::RegisterCompressorInitializedCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_compressorInitializedCallbacks = RegisterCallback(m_compressorInitializedCallbacks, "CompressorInitialized", callback, param, &newUid); @@ -162,7 +162,7 @@ int32_t PCMData::RegisterCompressorOnCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_compressorOnCallbacks = RegisterCallback( m_compressorOnCallbacks, "CompressorOn", callback, param, &newUid); } @@ -198,7 +198,7 @@ int32_t PCMData::RegisterClosedLoopEnabledCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_closedLoopEnabledCallbacks = RegisterCallback(m_closedLoopEnabledCallbacks, "ClosedLoopEnabled", callback, param, &newUid); @@ -236,7 +236,7 @@ int32_t PCMData::RegisterPressureSwitchCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_pressureSwitchCallbacks = RegisterCallback( m_pressureSwitchCallbacks, "PressureSwitch", callback, param, &newUid); } @@ -272,7 +272,7 @@ int32_t PCMData::RegisterCompressorCurrentCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_compressorCurrentCallbacks = RegisterCallback(m_compressorCurrentCallbacks, "CompressorCurrent", callback, param, &newUid); diff --git a/hal/src/main/native/sim/MockData/PCMDataInternal.h b/hal/src/main/native/sim/MockData/PCMDataInternal.h index 1e6655cca3..0651497613 100644 --- a/hal/src/main/native/sim/MockData/PCMDataInternal.h +++ b/hal/src/main/native/sim/MockData/PCMDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "../PortsInternal.h" #include "MockData/NotifyListenerVector.h" #include "MockData/PCMData.h" @@ -75,7 +77,7 @@ class PCMData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_solenoidInitialized[kNumSolenoidChannels]; std::shared_ptr m_solenoidInitializedCallbacks[kNumSolenoidChannels]; diff --git a/hal/src/main/native/sim/MockData/PDPData.cpp b/hal/src/main/native/sim/MockData/PDPData.cpp index 7caba9d4e6..7e20eeb9f8 100644 --- a/hal/src/main/native/sim/MockData/PDPData.cpp +++ b/hal/src/main/native/sim/MockData/PDPData.cpp @@ -32,7 +32,7 @@ int32_t PDPData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -68,7 +68,7 @@ int32_t PDPData::RegisterTemperatureCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_temperatureCallbacks = RegisterCallback( m_temperatureCallbacks, "Temperature", callback, param, &newUid); } @@ -103,7 +103,7 @@ int32_t PDPData::RegisterVoltageCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_voltageCallbacks = RegisterCallback(m_voltageCallbacks, "Voltage", callback, param, &newUid); } @@ -139,7 +139,7 @@ int32_t PDPData::RegisterCurrentCallback(int32_t channel, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_currentCallbacks[channel] = RegisterCallback( m_currentCallbacks[channel], "Current", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/PDPDataInternal.h b/hal/src/main/native/sim/MockData/PDPDataInternal.h index 8b81948d26..25fd7cf0c6 100644 --- a/hal/src/main/native/sim/MockData/PDPDataInternal.h +++ b/hal/src/main/native/sim/MockData/PDPDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "../PortsInternal.h" #include "MockData/NotifyListenerVector.h" #include "MockData/PDPData.h" @@ -48,7 +50,7 @@ class PDPData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_temperature{0.0}; diff --git a/hal/src/main/native/sim/MockData/PWMData.cpp b/hal/src/main/native/sim/MockData/PWMData.cpp index 32487e5616..c3e790fbda 100644 --- a/hal/src/main/native/sim/MockData/PWMData.cpp +++ b/hal/src/main/native/sim/MockData/PWMData.cpp @@ -34,7 +34,7 @@ int32_t PWMData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -69,7 +69,7 @@ int32_t PWMData::RegisterRawValueCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_rawValueCallbacks = RegisterCallback(m_rawValueCallbacks, "RawValue", callback, param, &newUid); } @@ -104,7 +104,7 @@ int32_t PWMData::RegisterSpeedCallback(HAL_NotifyCallback callback, void* param, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_speedCallbacks = RegisterCallback(m_speedCallbacks, "Speed", callback, param, &newUid); } @@ -139,7 +139,7 @@ int32_t PWMData::RegisterPositionCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_positionCallbacks = RegisterCallback(m_positionCallbacks, "Position", callback, param, &newUid); } @@ -175,7 +175,7 @@ int32_t PWMData::RegisterPeriodScaleCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_periodScaleCallbacks = RegisterCallback( m_periodScaleCallbacks, "PeriodScale", callback, param, &newUid); } @@ -211,7 +211,7 @@ int32_t PWMData::RegisterZeroLatchCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_zeroLatchCallbacks = RegisterCallback(m_zeroLatchCallbacks, "ZeroLatch", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/PWMDataInternal.h b/hal/src/main/native/sim/MockData/PWMDataInternal.h index ff04cc4ecc..7a09c6ae79 100644 --- a/hal/src/main/native/sim/MockData/PWMDataInternal.h +++ b/hal/src/main/native/sim/MockData/PWMDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/NotifyListenerVector.h" #include "MockData/PWMData.h" @@ -61,7 +63,7 @@ class PWMData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initialized{false}; std::shared_ptr m_initializedCallbacks = nullptr; std::atomic m_rawValue{0}; diff --git a/hal/src/main/native/sim/MockData/RelayData.cpp b/hal/src/main/native/sim/MockData/RelayData.cpp index 5ed33190fa..0adc11a2a5 100644 --- a/hal/src/main/native/sim/MockData/RelayData.cpp +++ b/hal/src/main/native/sim/MockData/RelayData.cpp @@ -29,7 +29,7 @@ int32_t RelayData::RegisterInitializedForwardCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedForwardCallbacks = RegisterCallback(m_initializedForwardCallbacks, "InitializedForward", callback, param, &newUid); @@ -66,7 +66,7 @@ int32_t RelayData::RegisterInitializedReverseCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedReverseCallbacks = RegisterCallback(m_initializedReverseCallbacks, "InitializedReverse", callback, param, &newUid); @@ -104,7 +104,7 @@ int32_t RelayData::RegisterForwardCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_forwardCallbacks = RegisterCallback(m_forwardCallbacks, "Forward", callback, param, &newUid); } @@ -140,7 +140,7 @@ int32_t RelayData::RegisterReverseCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_reverseCallbacks = RegisterCallback(m_reverseCallbacks, "Reverse", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/RelayDataInternal.h b/hal/src/main/native/sim/MockData/RelayDataInternal.h index 0c229e6b0b..9cd1ee72e9 100644 --- a/hal/src/main/native/sim/MockData/RelayDataInternal.h +++ b/hal/src/main/native/sim/MockData/RelayDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/NotifyListenerVector.h" #include "MockData/RelayData.h" @@ -49,7 +51,7 @@ class RelayData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_initializedForward{false}; std::shared_ptr m_initializedForwardCallbacks = nullptr; std::atomic m_initializedReverse{false}; diff --git a/hal/src/main/native/sim/MockData/RoboRioData.cpp b/hal/src/main/native/sim/MockData/RoboRioData.cpp index bc437c9a41..c50f48e7ec 100644 --- a/hal/src/main/native/sim/MockData/RoboRioData.cpp +++ b/hal/src/main/native/sim/MockData/RoboRioData.cpp @@ -52,7 +52,7 @@ int32_t RoboRioData::RegisterFPGAButtonCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_fPGAButtonCallbacks = RegisterCallback( m_fPGAButtonCallbacks, "FPGAButton", callback, param, &newUid); } @@ -88,7 +88,7 @@ int32_t RoboRioData::RegisterVInVoltageCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_vInVoltageCallbacks = RegisterCallback( m_vInVoltageCallbacks, "VInVoltage", callback, param, &newUid); } @@ -124,7 +124,7 @@ int32_t RoboRioData::RegisterVInCurrentCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_vInCurrentCallbacks = RegisterCallback( m_vInCurrentCallbacks, "VInCurrent", callback, param, &newUid); } @@ -160,7 +160,7 @@ int32_t RoboRioData::RegisterUserVoltage6VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userVoltage6VCallbacks = RegisterCallback( m_userVoltage6VCallbacks, "UserVoltage6V", callback, param, &newUid); } @@ -196,7 +196,7 @@ int32_t RoboRioData::RegisterUserCurrent6VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userCurrent6VCallbacks = RegisterCallback( m_userCurrent6VCallbacks, "UserCurrent6V", callback, param, &newUid); } @@ -232,7 +232,7 @@ int32_t RoboRioData::RegisterUserActive6VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userActive6VCallbacks = RegisterCallback( m_userActive6VCallbacks, "UserActive6V", callback, param, &newUid); } @@ -268,7 +268,7 @@ int32_t RoboRioData::RegisterUserVoltage5VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userVoltage5VCallbacks = RegisterCallback( m_userVoltage5VCallbacks, "UserVoltage5V", callback, param, &newUid); } @@ -304,7 +304,7 @@ int32_t RoboRioData::RegisterUserCurrent5VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userCurrent5VCallbacks = RegisterCallback( m_userCurrent5VCallbacks, "UserCurrent5V", callback, param, &newUid); } @@ -340,7 +340,7 @@ int32_t RoboRioData::RegisterUserActive5VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userActive5VCallbacks = RegisterCallback( m_userActive5VCallbacks, "UserActive5V", callback, param, &newUid); } @@ -376,7 +376,7 @@ int32_t RoboRioData::RegisterUserVoltage3V3Callback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userVoltage3V3Callbacks = RegisterCallback( m_userVoltage3V3Callbacks, "UserVoltage3V3", callback, param, &newUid); } @@ -412,7 +412,7 @@ int32_t RoboRioData::RegisterUserCurrent3V3Callback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userCurrent3V3Callbacks = RegisterCallback( m_userCurrent3V3Callbacks, "UserCurrent3V3", callback, param, &newUid); } @@ -448,7 +448,7 @@ int32_t RoboRioData::RegisterUserActive3V3Callback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userActive3V3Callbacks = RegisterCallback( m_userActive3V3Callbacks, "UserActive3V3", callback, param, &newUid); } @@ -484,7 +484,7 @@ int32_t RoboRioData::RegisterUserFaults6VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userFaults6VCallbacks = RegisterCallback( m_userFaults6VCallbacks, "UserFaults6V", callback, param, &newUid); } @@ -520,7 +520,7 @@ int32_t RoboRioData::RegisterUserFaults5VCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userFaults5VCallbacks = RegisterCallback( m_userFaults5VCallbacks, "UserFaults5V", callback, param, &newUid); } @@ -556,7 +556,7 @@ int32_t RoboRioData::RegisterUserFaults3V3Callback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_userFaults3V3Callbacks = RegisterCallback( m_userFaults3V3Callbacks, "UserFaults3V3", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/RoboRioDataInternal.h b/hal/src/main/native/sim/MockData/RoboRioDataInternal.h index e4dd8f8e4b..7b872eba24 100644 --- a/hal/src/main/native/sim/MockData/RoboRioDataInternal.h +++ b/hal/src/main/native/sim/MockData/RoboRioDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/NotifyListenerVector.h" #include "MockData/RoboRioData.h" @@ -124,7 +126,7 @@ class RoboRioData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_fPGAButton{false}; std::shared_ptr m_fPGAButtonCallbacks = nullptr; std::atomic m_vInVoltage{0.0}; diff --git a/hal/src/main/native/sim/MockData/SPIAccelerometerData.cpp b/hal/src/main/native/sim/MockData/SPIAccelerometerData.cpp index 43a2f70437..b99979b68b 100644 --- a/hal/src/main/native/sim/MockData/SPIAccelerometerData.cpp +++ b/hal/src/main/native/sim/MockData/SPIAccelerometerData.cpp @@ -31,7 +31,7 @@ int32_t SPIAccelerometerData::RegisterActiveCallback( if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_activeCallbacks = RegisterCallback(m_activeCallbacks, "Active", callback, param, &newUid); } @@ -67,7 +67,7 @@ int32_t SPIAccelerometerData::RegisterRangeCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_rangeCallbacks = RegisterCallback(m_rangeCallbacks, "Range", callback, param, &newUid); } @@ -103,7 +103,7 @@ int32_t SPIAccelerometerData::RegisterXCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_xCallbacks = RegisterCallback(m_xCallbacks, "X", callback, param, &newUid); } @@ -139,7 +139,7 @@ int32_t SPIAccelerometerData::RegisterYCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_yCallbacks = RegisterCallback(m_yCallbacks, "Y", callback, param, &newUid); } @@ -175,7 +175,7 @@ int32_t SPIAccelerometerData::RegisterZCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_zCallbacks = RegisterCallback(m_zCallbacks, "Z", callback, param, &newUid); } diff --git a/hal/src/main/native/sim/MockData/SPIAccelerometerDataInternal.h b/hal/src/main/native/sim/MockData/SPIAccelerometerDataInternal.h index fde1690e9b..e8c29587b3 100644 --- a/hal/src/main/native/sim/MockData/SPIAccelerometerDataInternal.h +++ b/hal/src/main/native/sim/MockData/SPIAccelerometerDataInternal.h @@ -10,6 +10,8 @@ #include #include +#include + #include "MockData/NotifyListenerVector.h" #include "MockData/SPIAccelerometerData.h" @@ -54,7 +56,7 @@ class SPIAccelerometerData { virtual void ResetData(); private: - std::mutex m_registerMutex; + wpi::mutex m_registerMutex; std::atomic m_active{false}; std::shared_ptr m_activeCallbacks = nullptr; std::atomic m_range{0}; diff --git a/hal/src/main/native/sim/MockData/SPIData.cpp b/hal/src/main/native/sim/MockData/SPIData.cpp index bc0628299c..69228566b9 100644 --- a/hal/src/main/native/sim/MockData/SPIData.cpp +++ b/hal/src/main/native/sim/MockData/SPIData.cpp @@ -35,7 +35,7 @@ int32_t SPIData::RegisterInitializedCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_initializedCallbacks = RegisterCallback( m_initializedCallbacks, "Initialized", callback, param, &newUid); } @@ -70,7 +70,7 @@ int32_t SPIData::RegisterReadCallback(HAL_BufferCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_readCallbacks = RegisterCallback(m_readCallbacks, "Read", callback, param, &newUid); } @@ -88,7 +88,7 @@ int32_t SPIData::RegisterWriteCallback(HAL_BufferCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_writeCallbacks = RegisterCallback(m_writeCallbacks, "Write", callback, param, &newUid); } @@ -107,7 +107,7 @@ int32_t SPIData::RegisterResetAccumulatorCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_resetAccumulatorCallback = RegisterCallback(m_resetAccumulatorCallback, "ResetAccumulator", callback, param, &newUid); @@ -131,7 +131,7 @@ int32_t SPIData::RegisterAccumulatorCallback(HAL_NotifyCallback callback, if (callback == nullptr) return -1; int32_t newUid = 0; { - std::lock_guard lock(m_registerMutex); + std::lock_guard lock(m_registerMutex); m_setAccumulatorCallback = RegisterCallback( m_setAccumulatorCallback, "SetAccumulator", callback, param, &newUid); } @@ -161,14 +161,14 @@ void SPIData::SetAccumulatorValue(int64_t value) { int64_t SPIData::GetAccumulatorValue() { return m_accumulatorValue; } int32_t SPIData::Read(uint8_t* buffer, int32_t count) { - std::lock_guard lock(m_dataMutex); + std::lock_guard lock(m_dataMutex); InvokeCallback(m_readCallbacks, "Read", buffer, count); return count; } int32_t SPIData::Write(uint8_t* dataToSend, int32_t sendSize) { - std::lock_guard lock(m_dataMutex); + std::lock_guard lock(m_dataMutex); InvokeCallback(m_writeCallbacks, "Write", dataToSend, sendSize); return sendSize; @@ -176,7 +176,7 @@ int32_t SPIData::Write(uint8_t* dataToSend, int32_t sendSize) { int32_t SPIData::Transaction(uint8_t* dataToSend, uint8_t* dataReceived, int32_t size) { - std::lock_guard lock(m_dataMutex); + std::lock_guard lock(m_dataMutex); return size; } diff --git a/hal/src/main/native/sim/MockData/SPIDataInternal.h b/hal/src/main/native/sim/MockData/SPIDataInternal.h index eeefdef0e7..e43f454b81 100644 --- a/hal/src/main/native/sim/MockData/SPIDataInternal.h +++ b/hal/src/main/native/sim/MockData/SPIDataInternal.h @@ -11,6 +11,8 @@ #include #include +#include + #include "MockData/NotifyListenerVector.h" #include "MockData/SPIData.h" @@ -52,8 +54,8 @@ class SPIData { void ResetData(); private: - std::mutex m_registerMutex; - std::mutex m_dataMutex; + wpi::mutex m_registerMutex; + wpi::mutex m_dataMutex; std::atomic m_initialized{false}; std::atomic m_accumulatorValue{false}; std::shared_ptr m_initializedCallbacks = nullptr; diff --git a/hal/src/main/native/sim/Notifier.cpp b/hal/src/main/native/sim/Notifier.cpp index 3ec3b4db79..afb3822a29 100644 --- a/hal/src/main/native/sim/Notifier.cpp +++ b/hal/src/main/native/sim/Notifier.cpp @@ -23,7 +23,7 @@ class NotifierThread : public wpi::SafeThread { public: void Main() { int32_t status = 0; - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); while (m_active) { startNotifierLoop: double waitTime = m_waitTime * 1e-6; diff --git a/wpilibc/src/main/native/cpp/CameraServer.cpp b/wpilibc/src/main/native/cpp/CameraServer.cpp index 28fbd46cc7..8299453179 100644 --- a/wpilibc/src/main/native/cpp/CameraServer.cpp +++ b/wpilibc/src/main/native/cpp/CameraServer.cpp @@ -65,7 +65,7 @@ static std::string MakeStreamValue(llvm::StringRef address, int port) { std::shared_ptr CameraServer::GetSourceTable( CS_Source source) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); return m_tables.lookup(source); } @@ -127,7 +127,7 @@ std::vector CameraServer::GetSourceStreamValues(CS_Source source) { } void CameraServer::UpdateStreamValues() { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); // Over all the sinks... for (const auto& i : m_sinks) { CS_Status status = 0; @@ -338,7 +338,7 @@ CameraServer::CameraServer() // Create subtable for the camera auto table = m_publishTable->GetSubTable(event.name); { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_tables.insert(std::make_pair(event.sourceHandle, table)); } llvm::SmallString<64> buf; @@ -591,7 +591,7 @@ void CameraServer::StartAutomaticCapture(const cs::VideoSource& camera) { cs::CvSink CameraServer::GetVideo() { cs::VideoSource source; { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_primarySourceName.empty()) { wpi_setWPIErrorWithContext(CameraServerError, "no camera available"); return cs::CvSink{}; @@ -611,7 +611,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) { name += camera.GetName(); { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); auto it = m_sinks.find(name); if (it != m_sinks.end()) { auto kind = it->second.GetKind(); @@ -635,7 +635,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) { cs::CvSink CameraServer::GetVideo(llvm::StringRef name) { cs::VideoSource source; { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); auto it = m_sources.find(name); if (it == m_sources.end()) { llvm::SmallString<64> buf; @@ -659,7 +659,7 @@ cs::CvSource CameraServer::PutVideo(llvm::StringRef name, int width, cs::MjpegServer CameraServer::AddServer(llvm::StringRef name) { int port; { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); port = m_nextPort++; } return AddServer(name, port); @@ -672,19 +672,19 @@ cs::MjpegServer CameraServer::AddServer(llvm::StringRef name, int port) { } void CameraServer::AddServer(const cs::VideoSink& server) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_sinks.emplace_second(server.GetName(), server); } void CameraServer::RemoveServer(llvm::StringRef name) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_sinks.erase(name); } cs::VideoSink CameraServer::GetServer() { llvm::SmallString<64> name; { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_primarySourceName.empty()) { wpi_setWPIErrorWithContext(CameraServerError, "no camera available"); return cs::VideoSink{}; @@ -696,7 +696,7 @@ cs::VideoSink CameraServer::GetServer() { } cs::VideoSink CameraServer::GetServer(llvm::StringRef name) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); auto it = m_sinks.find(name); if (it == m_sinks.end()) { llvm::SmallString<64> buf; @@ -710,18 +710,18 @@ cs::VideoSink CameraServer::GetServer(llvm::StringRef name) { void CameraServer::AddCamera(const cs::VideoSource& camera) { std::string name = camera.GetName(); - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_primarySourceName.empty()) m_primarySourceName = name; m_sources.emplace_second(name, camera); } void CameraServer::RemoveCamera(llvm::StringRef name) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); m_sources.erase(name); } void CameraServer::SetSize(int size) { - std::lock_guard lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_primarySourceName.empty()) return; auto it = m_sources.find(m_primarySourceName); if (it == m_sources.end()) return; diff --git a/wpilibc/src/main/native/cpp/Commands/Scheduler.cpp b/wpilibc/src/main/native/cpp/Commands/Scheduler.cpp index 93cad48522..eeb27a810f 100644 --- a/wpilibc/src/main/native/cpp/Commands/Scheduler.cpp +++ b/wpilibc/src/main/native/cpp/Commands/Scheduler.cpp @@ -40,7 +40,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; @@ -48,7 +48,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); } @@ -114,7 +114,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(); @@ -144,7 +144,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/src/main/native/cpp/DigitalGlitchFilter.cpp b/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp index 3e9d83bed1..a9bd112b3f 100644 --- a/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp +++ b/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp @@ -23,10 +23,10 @@ using namespace frc; std::array DigitalGlitchFilter::m_filterAllocated = { {false, false, false}}; -std::mutex DigitalGlitchFilter::m_mutex; +wpi::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()); @@ -39,7 +39,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/src/main/native/cpp/DriverStation.cpp b/wpilibc/src/main/native/cpp/DriverStation.cpp index b680584356..2a0321acfa 100644 --- a/wpilibc/src/main/native/cpp/DriverStation.cpp +++ b/wpilibc/src/main/native/cpp/DriverStation.cpp @@ -103,7 +103,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_cacheDataMutex); + std::unique_lock lock(m_cacheDataMutex); if (button > m_joystickButtons[stick].count) { // Unlock early so error printing isn't locked. lock.unlock(); @@ -134,7 +134,7 @@ bool DriverStation::GetStickButtonPressed(int stick, int button) { "ERROR: Button indexes begin at 1 in WPILib for C++ and Java"); return false; } - std::unique_lock lock(m_cacheDataMutex); + std::unique_lock lock(m_cacheDataMutex); if (button > m_joystickButtons[stick].count) { // Unlock early so error printing isn't locked. lock.unlock(); @@ -171,7 +171,7 @@ bool DriverStation::GetStickButtonReleased(int stick, int button) { "ERROR: Button indexes begin at 1 in WPILib for C++ and Java"); return false; } - std::unique_lock lock(m_cacheDataMutex); + std::unique_lock lock(m_cacheDataMutex); if (button > m_joystickButtons[stick].count) { // Unlock early so error printing isn't locked. lock.unlock(); @@ -204,7 +204,7 @@ double DriverStation::GetStickAxis(int stick, int axis) { wpi_setWPIError(BadJoystickIndex); return 0; } - std::unique_lock lock(m_cacheDataMutex); + std::unique_lock lock(m_cacheDataMutex); if (axis >= m_joystickAxes[stick].count) { // Unlock early so error printing isn't locked. m_cacheDataMutex.unlock(); @@ -230,7 +230,7 @@ int DriverStation::GetStickPOV(int stick, int pov) { wpi_setWPIError(BadJoystickIndex); return -1; } - std::unique_lock lock(m_cacheDataMutex); + std::unique_lock lock(m_cacheDataMutex); if (pov >= m_joystickPOVs[stick].count) { // Unlock early so error printing isn't locked. lock.unlock(); @@ -256,7 +256,7 @@ int DriverStation::GetStickButtons(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_joystickButtons[stick].buttons; } @@ -271,7 +271,7 @@ int DriverStation::GetStickAxisCount(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_joystickAxes[stick].count; } @@ -286,7 +286,7 @@ int DriverStation::GetStickPOVCount(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_joystickPOVs[stick].count; } @@ -301,7 +301,7 @@ int DriverStation::GetStickButtonCount(int stick) const { wpi_setWPIError(BadJoystickIndex); return 0; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_joystickButtons[stick].count; } @@ -316,7 +316,7 @@ bool DriverStation::GetJoystickIsXbox(int stick) const { wpi_setWPIError(BadJoystickIndex); return false; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return static_cast(m_joystickDescriptor[stick].isXbox); } @@ -331,7 +331,7 @@ int DriverStation::GetJoystickType(int stick) const { wpi_setWPIError(BadJoystickIndex); return -1; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return static_cast(m_joystickDescriptor[stick].type); } @@ -345,7 +345,7 @@ std::string DriverStation::GetJoystickName(int stick) const { if (stick >= kJoystickPorts) { wpi_setWPIError(BadJoystickIndex); } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); std::string retVal(m_joystickDescriptor[stick].name); return retVal; } @@ -361,7 +361,7 @@ int DriverStation::GetJoystickAxisType(int stick, int axis) const { wpi_setWPIError(BadJoystickIndex); return -1; } - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_joystickDescriptor[stick].axisTypes[axis]; } @@ -482,27 +482,27 @@ bool DriverStation::IsBrownedOut() const { } std::string DriverStation::GetGameSpecificMessage() const { - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_matchInfo->gameSpecificMessage; } std::string DriverStation::GetEventName() const { - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_matchInfo->eventName; } DriverStation::MatchType DriverStation::GetMatchType() const { - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_matchInfo->matchType; } int DriverStation::GetMatchNumber() const { - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_matchInfo->matchNumber; } int DriverStation::GetReplayNumber() const { - std::lock_guard lock(m_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); return m_matchInfo->replayNumber; } @@ -650,7 +650,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_cacheDataMutex); + std::lock_guard lock(m_cacheDataMutex); for (int32_t i = 0; i < kJoystickPorts; i++) { // If buttons weren't pressed and are now, set flags in m_buttonsPressed @@ -768,7 +768,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/src/main/native/cpp/ErrorBase.cpp b/wpilibc/src/main/native/cpp/ErrorBase.cpp index 9e38a183d1..24faeaac56 100644 --- a/wpilibc/src/main/native/cpp/ErrorBase.cpp +++ b/wpilibc/src/main/native/cpp/ErrorBase.cpp @@ -22,7 +22,7 @@ using namespace frc; -std::mutex ErrorBase::_globalErrorMutex; +wpi::mutex ErrorBase::_globalErrorMutex; Error ErrorBase::_globalError; ErrorBase::ErrorBase() { HAL_Initialize(500, 0); } @@ -68,7 +68,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); } @@ -97,7 +97,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); } @@ -122,7 +122,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); } @@ -160,7 +160,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); } @@ -186,7 +186,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); } @@ -208,7 +208,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, @@ -222,7 +222,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(); } @@ -233,6 +233,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/src/main/native/cpp/MotorSafetyHelper.cpp b/wpilibc/src/main/native/cpp/MotorSafetyHelper.cpp index a0ca20859e..b8fc6b3dfb 100644 --- a/wpilibc/src/main/native/cpp/MotorSafetyHelper.cpp +++ b/wpilibc/src/main/native/cpp/MotorSafetyHelper.cpp @@ -18,7 +18,7 @@ using namespace frc; std::set MotorSafetyHelper::m_helperList; -std::mutex MotorSafetyHelper::m_listMutex; +wpi::mutex MotorSafetyHelper::m_listMutex; /** * The constructor for a MotorSafetyHelper object. @@ -38,12 +38,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; } @@ -61,7 +61,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; } @@ -70,7 +70,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; } @@ -80,7 +80,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(); } @@ -94,7 +94,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()) { llvm::SmallString<128> buf; llvm::raw_svector_ostream desc(buf); @@ -111,7 +111,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; } @@ -121,7 +121,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; } @@ -131,7 +131,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/src/main/native/cpp/Notifier.cpp b/wpilibc/src/main/native/cpp/Notifier.cpp index b8c5c544f0..abba304a05 100644 --- a/wpilibc/src/main/native/cpp/Notifier.cpp +++ b/wpilibc/src/main/native/cpp/Notifier.cpp @@ -15,7 +15,7 @@ using namespace frc; -std::mutex Notifier::m_destructorMutex; +wpi::mutex Notifier::m_destructorMutex; /** * Create a Notifier for timer event notification. @@ -45,8 +45,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); } /** @@ -69,7 +69,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; @@ -96,7 +96,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; @@ -114,7 +114,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; @@ -137,6 +137,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/src/main/native/cpp/PIDController.cpp b/wpilibc/src/main/native/cpp/PIDController.cpp index 2aac12b910..db30d5a6ec 100644 --- a/wpilibc/src/main/native/cpp/PIDController.cpp +++ b/wpilibc/src/main/native/cpp/PIDController.cpp @@ -91,7 +91,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; @@ -103,7 +103,7 @@ void PIDController::Calculate() { if (enabled) { double feedForward = CalculateFeedForward(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); double input = pidInput->PIDGet(); double result; PIDOutput* pidOutput; @@ -200,7 +200,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; @@ -223,7 +223,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; @@ -242,7 +242,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; } @@ -252,7 +252,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; } @@ -262,7 +262,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; } @@ -272,7 +272,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; } @@ -284,7 +284,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; } @@ -298,7 +298,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; } @@ -310,7 +310,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; } @@ -325,7 +325,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; } @@ -339,7 +339,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) @@ -366,7 +366,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; } @@ -376,7 +376,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(); } @@ -388,7 +388,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()); } } @@ -419,7 +419,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(); } @@ -433,7 +433,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; } @@ -445,7 +445,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; } @@ -457,7 +457,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; } @@ -473,7 +473,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. @@ -496,13 +496,13 @@ void PIDController::SetToleranceBuffer(int bufLength) { */ 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(); - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); switch (m_toleranceType) { case kPercentTolerance: return std::fabs(error) < @@ -523,7 +523,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; } @@ -547,7 +547,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; } @@ -557,7 +557,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; @@ -586,7 +586,7 @@ void PIDController::InitTable(std::shared_ptr subtable) { m_pListener = m_pEntry.AddListener( [=](const nt::EntryNotification& event) { if (!event.value->IsDouble()) return; - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_P = event.value->GetDouble(); }, NT_NOTIFY_NEW | NT_NOTIFY_UPDATE); @@ -594,7 +594,7 @@ void PIDController::InitTable(std::shared_ptr subtable) { m_iListener = m_iEntry.AddListener( [=](const nt::EntryNotification& event) { if (!event.value->IsDouble()) return; - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_I = event.value->GetDouble(); }, NT_NOTIFY_NEW | NT_NOTIFY_UPDATE); @@ -602,7 +602,7 @@ void PIDController::InitTable(std::shared_ptr subtable) { m_dListener = m_dEntry.AddListener( [=](const nt::EntryNotification& event) { if (!event.value->IsDouble()) return; - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_D = event.value->GetDouble(); }, NT_NOTIFY_NEW | NT_NOTIFY_UPDATE); @@ -610,7 +610,7 @@ void PIDController::InitTable(std::shared_ptr subtable) { m_fListener = m_fEntry.AddListener( [=](const nt::EntryNotification& event) { if (!event.value->IsDouble()) return; - std::lock_guard sync(m_mutex); + std::lock_guard sync(m_mutex); m_F = event.value->GetDouble(); }, NT_NOTIFY_NEW | NT_NOTIFY_UPDATE); diff --git a/wpilibc/src/main/native/cpp/Resource.cpp b/wpilibc/src/main/native/cpp/Resource.cpp index 7f96ba9793..e266e6dabb 100644 --- a/wpilibc/src/main/native/cpp/Resource.cpp +++ b/wpilibc/src/main/native/cpp/Resource.cpp @@ -12,7 +12,7 @@ using namespace frc; -std::mutex Resource::m_createMutex; +wpi::mutex Resource::m_createMutex; /** * Allocate storage for a new instance of Resource. @@ -38,7 +38,7 @@ Resource::Resource(uint32_t elements) { */ void Resource::CreateResourceObject(std::unique_ptr& r, uint32_t elements) { - std::lock_guard sync(m_createMutex); + std::lock_guard sync(m_createMutex); if (!r) { r = std::make_unique(elements); } @@ -52,7 +52,7 @@ void Resource::CreateResourceObject(std::unique_ptr& r, * allocated. */ uint32_t Resource::Allocate(const std::string& resourceDesc) { - std::lock_guard sync(m_allocateMutex); + std::lock_guard sync(m_allocateMutex); for (uint32_t i = 0; i < m_isAllocated.size(); i++) { if (!m_isAllocated[i]) { m_isAllocated[i] = true; @@ -70,7 +70,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_allocateMutex); + std::lock_guard sync(m_allocateMutex); if (index >= m_isAllocated.size()) { wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); return std::numeric_limits::max(); @@ -91,7 +91,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_allocateMutex); + std::unique_lock sync(m_allocateMutex); if (index == std::numeric_limits::max()) return; if (index >= m_isAllocated.size()) { wpi_setWPIError(NotAllocated); diff --git a/wpilibc/src/main/native/cpp/Timer.cpp b/wpilibc/src/main/native/cpp/Timer.cpp index 36b040b06a..ec9e6ec666 100644 --- a/wpilibc/src/main/native/cpp/Timer.cpp +++ b/wpilibc/src/main/native/cpp/Timer.cpp @@ -83,7 +83,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 @@ -107,7 +107,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(); } @@ -119,7 +119,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; @@ -136,7 +136,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; @@ -153,7 +153,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/src/main/native/include/CameraServer.h b/wpilibc/src/main/native/include/CameraServer.h index 5672a83d2c..bf9d51958c 100644 --- a/wpilibc/src/main/native/include/CameraServer.h +++ b/wpilibc/src/main/native/include/CameraServer.h @@ -11,13 +11,13 @@ #include #include -#include #include #include #include #include #include +#include #include "ErrorBase.h" #include "cscore.h" @@ -300,7 +300,7 @@ class CameraServer : public ErrorBase { static constexpr char const* kPublishName = "/CameraPublisher"; - std::mutex m_mutex; + wpi::mutex m_mutex; std::atomic m_defaultUsbDevice; std::string m_primarySourceName; llvm::StringMap m_sources; diff --git a/wpilibc/src/main/native/include/Commands/Scheduler.h b/wpilibc/src/main/native/include/Commands/Scheduler.h index e773c1f2f1..d821a06b9d 100644 --- a/wpilibc/src/main/native/include/Commands/Scheduler.h +++ b/wpilibc/src/main/native/include/Commands/Scheduler.h @@ -10,11 +10,12 @@ #include #include #include -#include #include #include #include +#include + #include "Commands/Command.h" #include "ErrorBase.h" #include "SmartDashboard/NamedSendable.h" @@ -53,11 +54,11 @@ class Scheduler : public ErrorBase, public NamedSendable { void ProcessCommandAddition(Command* command); Command::SubsystemSet m_subsystems; - std::mutex m_buttonsLock; + wpi::mutex m_buttonsLock; typedef std::vector ButtonVector; ButtonVector m_buttons; typedef std::vector CommandVector; - std::mutex m_additionsLock; + wpi::mutex m_additionsLock; CommandVector m_additions; typedef std::set CommandSet; CommandSet m_commands; diff --git a/wpilibc/src/main/native/include/DigitalGlitchFilter.h b/wpilibc/src/main/native/include/DigitalGlitchFilter.h index 9819c39a9c..fc6a186d43 100644 --- a/wpilibc/src/main/native/include/DigitalGlitchFilter.h +++ b/wpilibc/src/main/native/include/DigitalGlitchFilter.h @@ -10,7 +10,8 @@ #include #include -#include + +#include #include "DigitalSource.h" @@ -51,7 +52,7 @@ class DigitalGlitchFilter : public SensorBase { void DoAdd(DigitalSource* input, int requested_index); int m_channelIndex = -1; - static std::mutex m_mutex; + static wpi::mutex m_mutex; static std::array m_filterAllocated; }; diff --git a/wpilibc/src/main/native/include/DriverStation.h b/wpilibc/src/main/native/include/DriverStation.h index cdc565a524..b0af2979f4 100644 --- a/wpilibc/src/main/native/include/DriverStation.h +++ b/wpilibc/src/main/native/include/DriverStation.h @@ -10,12 +10,12 @@ #include #include #include -#include #include #include #include #include +#include #include "RobotState.h" #include "SensorBase.h" @@ -135,7 +135,7 @@ class DriverStation : public SensorBase, public RobotStateInterface { std::thread m_dsThread; std::atomic m_isRunning{false}; - mutable std::mutex m_cacheDataMutex; + mutable wpi::mutex m_cacheDataMutex; // Robot state status variables bool m_userInDisabled = false; @@ -146,7 +146,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 std::mutex m_controlWordMutex; + mutable wpi::mutex m_controlWordMutex; double m_nextMessageTime = 0; }; diff --git a/wpilibc/src/main/native/include/ErrorBase.h b/wpilibc/src/main/native/include/ErrorBase.h index dfe758237f..1cd918c0a1 100644 --- a/wpilibc/src/main/native/include/ErrorBase.h +++ b/wpilibc/src/main/native/include/ErrorBase.h @@ -7,9 +7,8 @@ #pragma once -#include - #include +#include #include "Base.h" #include "Error.h" @@ -115,7 +114,7 @@ class ErrorBase { protected: mutable Error m_error; // TODO: Replace globalError with a global list of all errors. - static std::mutex _globalErrorMutex; + static wpi::mutex _globalErrorMutex; static Error _globalError; }; diff --git a/wpilibc/src/main/native/include/MotorSafetyHelper.h b/wpilibc/src/main/native/include/MotorSafetyHelper.h index dbf4af8149..277ad64212 100644 --- a/wpilibc/src/main/native/include/MotorSafetyHelper.h +++ b/wpilibc/src/main/native/include/MotorSafetyHelper.h @@ -7,9 +7,10 @@ #pragma once -#include #include +#include + #include "ErrorBase.h" namespace frc { @@ -37,13 +38,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 std::mutex m_syncMutex; + mutable wpi::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 std::mutex m_listMutex; + static wpi::mutex m_listMutex; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/Notifier.h b/wpilibc/src/main/native/include/Notifier.h index d77246aeee..8a70247ee3 100644 --- a/wpilibc/src/main/native/include/Notifier.h +++ b/wpilibc/src/main/native/include/Notifier.h @@ -11,10 +11,10 @@ #include #include -#include #include #include +#include #include "ErrorBase.h" @@ -47,9 +47,9 @@ class Notifier : public ErrorBase { static void Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle); // used to constrain execution between destructors and callback - static std::mutex m_destructorMutex; + static wpi::mutex m_destructorMutex; // held while updating process information - std::mutex m_processMutex; + wpi::mutex m_processMutex; // HAL handle, atomic for proper destruction std::atomic m_notifier{0}; // address of the handler diff --git a/wpilibc/src/main/native/include/PIDController.h b/wpilibc/src/main/native/include/PIDController.h index 7d657a6fe6..e2415971eb 100644 --- a/wpilibc/src/main/native/include/PIDController.h +++ b/wpilibc/src/main/native/include/PIDController.h @@ -9,10 +9,11 @@ #include #include -#include #include #include +#include + #include "Base.h" #include "Controller.h" #include "LiveWindow/LiveWindow.h" @@ -147,7 +148,7 @@ class PIDController : public LiveWindowSendable, public PIDInterface { std::queue m_buf; double m_bufTotal = 0; - mutable std::mutex m_mutex; + mutable wpi::mutex m_mutex; std::unique_ptr m_controlLoop; Timer m_setpointTimer; diff --git a/wpilibc/src/main/native/include/Resource.h b/wpilibc/src/main/native/include/Resource.h index 057505929b..367f9aceb2 100644 --- a/wpilibc/src/main/native/include/Resource.h +++ b/wpilibc/src/main/native/include/Resource.h @@ -10,10 +10,11 @@ #include #include -#include #include #include +#include + #include "ErrorBase.h" namespace frc { @@ -43,9 +44,9 @@ class Resource : public ErrorBase { private: std::vector m_isAllocated; - std::mutex m_allocateMutex; + wpi::mutex m_allocateMutex; - static std::mutex m_createMutex; + static wpi::mutex m_createMutex; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/Timer.h b/wpilibc/src/main/native/include/Timer.h index feb61e6379..3b62fcfb28 100644 --- a/wpilibc/src/main/native/include/Timer.h +++ b/wpilibc/src/main/native/include/Timer.h @@ -7,7 +7,7 @@ #pragma once -#include +#include #include "Base.h" @@ -52,7 +52,7 @@ class Timer { double m_startTime = 0.0; double m_accumulatedTime = 0.0; bool m_running = false; - mutable std::mutex m_mutex; + mutable wpi::mutex m_mutex; }; } // namespace frc diff --git a/wpilibcIntegrationTests/src/FRCUserProgram/cpp/MutexTest.cpp b/wpilibcIntegrationTests/src/FRCUserProgram/cpp/MutexTest.cpp deleted file mode 100644 index 3561529c66..0000000000 --- a/wpilibcIntegrationTests/src/FRCUserProgram/cpp/MutexTest.cpp +++ /dev/null @@ -1,270 +0,0 @@ -/*----------------------------------------------------------------------------*/ -/* Copyright (c) 2016-2017 FIRST. All Rights Reserved. */ -/* Open Source Software - may be modified and shared by FRC teams. The code */ -/* must be accompanied by the FIRST BSD license file in the root directory of */ -/* the project. */ -/*----------------------------------------------------------------------------*/ - -#include // NOLINT(build/include_order) - -#include -#include -#include - -#include "TestBench.h" -#include "gtest/gtest.h" - -namespace wpilib { -namespace testing { - -using std::chrono::system_clock; - -// Threading primitive used to notify many threads that a condition is now true. -// The condition can not be cleared. -class Notification { - public: - // Efficiently waits until the Notification has been notified once. - void Wait() { - 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); - m_set = true; - m_condition.notify_all(); - } - - private: - // 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. - bool m_set = false; -}; - -void SetProcessorAffinity(int32_t core_id) { - cpu_set_t cpuset; - CPU_ZERO(&cpuset); - CPU_SET(core_id, &cpuset); - - pthread_t current_thread = pthread_self(); - ASSERT_EQ(pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset), - 0); -} - -void SetThreadRealtimePriorityOrDie(int32_t priority) { - struct sched_param param; - // Set realtime priority for this thread - param.sched_priority = priority + sched_get_priority_min(SCHED_RR); - ASSERT_EQ(pthread_setschedparam(pthread_self(), SCHED_RR, ¶m), 0) - << ": Failed to set scheduler priority."; -} - -// This thread holds the mutex and spins until signaled to release it and stop. -template -class LowPriorityThread { - public: - explicit LowPriorityThread(MutexType* mutex) - : m_mutex(mutex), m_hold_mutex(1), m_success(0) {} - - void operator()() { - SetProcessorAffinity(0); - SetThreadRealtimePriorityOrDie(20); - m_mutex->lock(); - m_started.Notify(); - while (m_hold_mutex.test_and_set()) { - } - m_mutex->unlock(); - m_success.store(1); - } - - void WaitForStartup() { m_started.Wait(); } - void release_mutex() { m_hold_mutex.clear(); } - bool success() { return m_success.load(); } - - private: - // 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. - // Using a mutex to protect something could cause other issues, and a delay - // between setting and reading isn't a problem as long as the set is atomic. - std::atomic_flag m_hold_mutex; - std::atomic m_success; -}; - -// This thread spins forever until signaled to stop. -class BusyWaitingThread { - public: - BusyWaitingThread() : m_run(1), m_success(0) {} - - void operator()() { - SetProcessorAffinity(0); - SetThreadRealtimePriorityOrDie(21); - system_clock::time_point start_time = system_clock::now(); - m_started.Notify(); - while (m_run.test_and_set()) { - // Have the busy waiting thread time out after a while. If it times out, - // the test failed. - if (system_clock::now() - start_time > std::chrono::milliseconds(50)) { - return; - } - } - m_success.store(1); - } - - void quit() { m_run.clear(); } - void WaitForStartup() { m_started.Wait(); } - bool success() { return m_success.load(); } - - private: - // Use an atomic type to signal if the thread should be running or not. A - // mutex could affect the scheduler, which isn't worth it. A delay between - // setting and reading the new value is fine. - std::atomic_flag m_run; - - Notification m_started; - - std::atomic m_success; -}; - -// This thread starts up, grabs the mutex, and then exits. -template -class HighPriorityThread { - public: - explicit HighPriorityThread(MutexType* mutex) : m_mutex(mutex) {} - - void operator()() { - SetProcessorAffinity(0); - SetThreadRealtimePriorityOrDie(22); - m_started.Notify(); - m_mutex->lock(); - m_success.store(1); - } - - void WaitForStartup() { m_started.Wait(); } - bool success() { return m_success.load(); } - - private: - Notification m_started; - MutexType* m_mutex; - std::atomic m_success{0}; -}; - -// Class to test a MutexType to see if it solves the priority inheritance -// problem. -// -// To run the test, we need 3 threads, and then 1 thread to kick the test off. -// The threads must all run on the same core, otherwise they wouldn't starve -// eachother. The threads and their roles are as follows: -// -// Low priority thread: -// Holds a lock that the high priority thread needs, and releases it upon -// request. -// Medium priority thread: -// Hogs the processor so that the low priority thread will never run if it's -// priority doesn't get bumped. -// High priority thread: -// Starts up and then goes to grab the lock that the low priority thread has. -// -// Control thread: -// Sets the deadlock up so that it will happen 100% of the time by making sure -// that each thread in order gets to the point that it needs to be at to cause -// the deadlock. -template -class InversionTestRunner { - public: - void operator()() { - // This thread must run at the highest priority or it can't coordinate the - // inversion. This means that it can't busy wait or everything could - // starve. - SetThreadRealtimePriorityOrDie(23); - - MutexType m; - - // Start the lowest priority thread and wait until it holds the lock. - LowPriorityThread low(&m); - std::thread low_thread(std::ref(low)); - low.WaitForStartup(); - - // Start the busy waiting thread and let it get to the loop. - BusyWaitingThread busy; - std::thread busy_thread(std::ref(busy)); - busy.WaitForStartup(); - - // Start the high priority thread and let it become blocked on the lock. - HighPriorityThread high(&m); - std::thread high_thread(std::ref(high)); - high.WaitForStartup(); - // Startup and locking the mutex in the high priority thread aren't atomic, - // but pretty close. Wait a bit to let it happen. - std::this_thread::sleep_for(std::chrono::milliseconds(10)); - - // Release the mutex in the low priority thread. If done right, everything - // should finish now. - low.release_mutex(); - - // Wait for everything to finish and compute success. - high_thread.join(); - busy.quit(); - busy_thread.join(); - low_thread.join(); - m_success = low.success() && busy.success() && high.success(); - } - - bool success() { return m_success; } - - private: - bool m_success = false; -}; - -// TODO: Fix roborio permissions to run as root. - -// Priority inversion test. -TEST(MutexTest, DISABLED_PriorityInversionTest) { - InversionTestRunner runner; - std::thread runner_thread(std::ref(runner)); - runner_thread.join(); - EXPECT_TRUE(runner.success()); -} - -// Verify that the non-priority inversion mutex doesn't pass the test. -TEST(MutexTest, DISABLED_StdMutexPriorityInversionTest) { - InversionTestRunner runner; - std::thread runner_thread(std::ref(runner)); - runner_thread.join(); - EXPECT_FALSE(runner.success()); -} - -// Smoke test to make sure that mutexes lock and unlock. -TEST(MutexTest, TryLock) { - hal::priority_mutex m; - m.lock(); - EXPECT_FALSE(m.try_lock()); - m.unlock(); - EXPECT_TRUE(m.try_lock()); -} - -// Priority inversion test. -TEST(MutexTest, DISABLED_ReentrantPriorityInversionTest) { - InversionTestRunner runner; - std::thread runner_thread(std::ref(runner)); - runner_thread.join(); - EXPECT_TRUE(runner.success()); -} - -// Smoke test to make sure that mutexes lock and unlock. -TEST(MutexTest, ReentrantTryLock) { - hal::priority_recursive_mutex m; - m.lock(); - EXPECT_TRUE(m.try_lock()); - m.unlock(); - EXPECT_TRUE(m.try_lock()); -} - -} // namespace testing -} // namespace wpilib diff --git a/wpilibj/src/main/native/cpp/InterruptJNI.cpp b/wpilibj/src/main/native/cpp/InterruptJNI.cpp index 7f37b5382d..6150116f3d 100644 --- a/wpilibj/src/main/native/cpp/InterruptJNI.cpp +++ b/wpilibj/src/main/native/cpp/InterruptJNI.cpp @@ -8,9 +8,10 @@ #include #include #include -#include -#include #include + +#include + #include "HAL/cpp/Log.h" #include "HAL/Interrupts.h" @@ -87,7 +88,7 @@ void InterruptThreadJNI::Main() { &args); if (rs != JNI_OK) return; - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); while (m_active) { m_cond.wait(lock, [&] { return !m_active || m_notify; }); if (!m_active) break; diff --git a/wpilibj/src/main/native/cpp/NotifierJNI.cpp b/wpilibj/src/main/native/cpp/NotifierJNI.cpp index 16886dbb4c..0bbccfda26 100644 --- a/wpilibj/src/main/native/cpp/NotifierJNI.cpp +++ b/wpilibj/src/main/native/cpp/NotifierJNI.cpp @@ -10,10 +10,9 @@ #include #include #include -#include #include -#include #include +#include #include "HALUtil.h" #include "HAL/cpp/Log.h" #include "edu_wpi_first_wpilibj_hal_NotifierJNI.h" @@ -86,7 +85,7 @@ void NotifierThreadJNI::Main() { jvm->AttachCurrentThreadAsDaemon(reinterpret_cast(&env), &args); if (rs != JNI_OK) return; - std::unique_lock lock(m_mutex); + std::unique_lock lock(m_mutex); while (m_active) { m_cond.wait(lock, [&] { return !m_active || m_notify; }); if (!m_active) break;