diff --git a/hal/include/HAL/cpp/priority_mutex.h b/hal/include/HAL/cpp/priority_mutex.h index 719c4df4f7..16e88f64ab 100644 --- a/hal/include/HAL/cpp/priority_mutex.h +++ b/hal/include/HAL/cpp/priority_mutex.h @@ -1,6 +1,6 @@ #pragma once -// Allows usage with std::unique_lock without including separately +// Allows usage with std::lock_guard without including separately #include #ifdef FRC_SIMULATOR diff --git a/hal/lib/Athena/Analog.cpp b/hal/lib/Athena/Analog.cpp index 02d52b9873..9c644f91c3 100644 --- a/hal/lib/Athena/Analog.cpp +++ b/hal/lib/Athena/Analog.cpp @@ -41,7 +41,7 @@ bool analogSystemInitialized = false; * Initialize the analog System. */ void initializeAnalog(int32_t *status) { - std::unique_lock sync(analogRegisterWindowMutex); + std::lock_guard sync(analogRegisterWindowMutex); if (analogSystemInitialized) return; analogInputSystem = tAI::create(status); analogOutputSystem = tAO::create(status); @@ -265,7 +265,7 @@ int16_t getAnalogValue(void* analog_port_pointer, int32_t *status) { readSelect.Averaged = false; { - std::unique_lock sync(analogRegisterWindowMutex); + std::lock_guard sync(analogRegisterWindowMutex); analogInputSystem->writeReadSelect(readSelect, status); analogInputSystem->strobeLatchOutput(status); value = (int16_t) analogInputSystem->readOutput(status); @@ -296,7 +296,7 @@ int32_t getAnalogAverageValue(void* analog_port_pointer, int32_t *status) { readSelect.Averaged = true; { - std::unique_lock sync(analogRegisterWindowMutex); + std::lock_guard sync(analogRegisterWindowMutex); analogInputSystem->writeReadSelect(readSelect, status); analogInputSystem->strobeLatchOutput(status); value = (int32_t) analogInputSystem->readOutput(status); diff --git a/hal/lib/Athena/Digital.cpp b/hal/lib/Athena/Digital.cpp index d095d7523a..fdbd61a329 100644 --- a/hal/lib/Athena/Digital.cpp +++ b/hal/lib/Athena/Digital.cpp @@ -284,7 +284,7 @@ void setPWMDutyCycle(void* pwmGenerator, double dutyCycle, int32_t *status) { float rawDutyCycle = 256.0 * dutyCycle; if (rawDutyCycle > 255.5) rawDutyCycle = 255.5; { - std::unique_lock sync(digitalPwmMutex); + std::lock_guard sync(digitalPwmMutex); uint8_t pwmPeriodPower = digitalSystem->readPWMPeriodPower(status); if (pwmPeriodPower < 4) { // The resolution of the duty cycle drops close to the highest frequencies. @@ -318,7 +318,7 @@ void setRelayForward(void* digital_port_pointer, bool on, int32_t *status) { DigitalPort* port = (DigitalPort*) digital_port_pointer; checkRelayChannel(port); { - std::unique_lock sync(digitalRelayMutex); + std::lock_guard sync(digitalRelayMutex); uint8_t forwardRelays = relaySystem->readValue_Forward(status); if (on) forwardRelays |= 1 << port->port.pin; @@ -337,7 +337,7 @@ void setRelayReverse(void* digital_port_pointer, bool on, int32_t *status) { DigitalPort* port = (DigitalPort*) digital_port_pointer; checkRelayChannel(port); { - std::unique_lock sync(digitalRelayMutex); + std::lock_guard sync(digitalRelayMutex); uint8_t reverseRelays = relaySystem->readValue_Reverse(status); if (on) reverseRelays |= 1 << port->port.pin; @@ -384,7 +384,7 @@ bool allocateDIO(void* digital_port_pointer, bool input, int32_t *status) { } { - std::unique_lock sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); tDIO::tOutputEnable outputEnable = digitalSystem->readOutputEnable(status); @@ -468,7 +468,7 @@ void setDIO(void* digital_port_pointer, short value, int32_t *status) { value = 1; } { - std::unique_lock sync(digitalDIOMutex); + std::lock_guard sync(digitalDIOMutex); tDIO::tDO currentDIO = digitalSystem->readDO(status); if(port->port.pin < kNumHeaders) { @@ -1146,7 +1146,7 @@ void spiInitialize(uint8_t port, int32_t *status) { */ int32_t spiTransaction(uint8_t port, uint8_t *dataToSend, uint8_t *dataReceived, uint8_t size) { - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); return spilib_writeread(spiGetHandle(port), (const char*) dataToSend, (char*) dataReceived, (int32_t) size); } @@ -1162,7 +1162,7 @@ int32_t spiTransaction(uint8_t port, uint8_t *dataToSend, uint8_t *dataReceived, */ int32_t spiWrite(uint8_t port, uint8_t* dataToSend, uint8_t sendSize) { - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); return spilib_write(spiGetHandle(port), (const char*) dataToSend, (int32_t) sendSize); } @@ -1180,7 +1180,7 @@ int32_t spiWrite(uint8_t port, uint8_t* dataToSend, uint8_t sendSize) */ int32_t spiRead(uint8_t port, uint8_t *buffer, uint8_t count) { - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); return spilib_read(spiGetHandle(port), (char*) buffer, (int32_t) count); } @@ -1190,7 +1190,7 @@ int32_t spiRead(uint8_t port, uint8_t *buffer, uint8_t count) * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP */ void spiClose(uint8_t port) { - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); spilib_close(spiGetHandle(port)); spiSetHandle(port, 0); return; @@ -1203,7 +1203,7 @@ void spiClose(uint8_t port) { * @param speed The speed in Hz (0-1MHz) */ void spiSetSpeed(uint8_t port, uint32_t speed) { - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); spilib_setspeed(spiGetHandle(port), speed); } @@ -1216,7 +1216,7 @@ void spiSetSpeed(uint8_t port, uint32_t speed) { * @param clk_idle_high True to set the clock to active low, False to set the clock active high */ void spiSetOpts(uint8_t port, int msb_first, int sample_on_trailing, int clk_idle_high) { - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); spilib_setopts(spiGetHandle(port), msb_first, sample_on_trailing, clk_idle_high); } @@ -1226,7 +1226,7 @@ void spiSetOpts(uint8_t port, int msb_first, int sample_on_trailing, int clk_idl * @param port The number of the port to use. 0-3 for Onboard CS0-CS2, 4 for MXP */ void spiSetChipSelectActiveHigh(uint8_t port, int32_t *status){ - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); if(port < 4) { spiSystem->writeChipSelectActiveHigh_Hdr(spiSystem->readChipSelectActiveHigh_Hdr(status) | (1< sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); if(port < 4) { spiSystem->writeChipSelectActiveHigh_Hdr(spiSystem->readChipSelectActiveHigh_Hdr(status) & ~(1< sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); switch(port){ case 0: return m_spiCS0Handle; @@ -1285,7 +1285,7 @@ int32_t spiGetHandle(uint8_t port){ * @param handle The value of the handle for the port. */ void spiSetHandle(uint8_t port, int32_t handle){ - std::unique_lock sync(spiGetSemaphore(port)); + std::lock_guard sync(spiGetSemaphore(port)); switch(port){ case 0: m_spiCS0Handle = handle; @@ -1336,7 +1336,7 @@ void i2CInitialize(uint8_t port, int32_t *status) { priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::unique_lock sync(lock); + std::lock_guard sync(lock); if(port == 0) { i2COnboardObjCount++; if (i2COnBoardHandle > 0) return; @@ -1375,7 +1375,7 @@ int32_t i2CTransaction(uint8_t port, uint8_t deviceAddress, uint8_t *dataToSend, priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::unique_lock sync(lock); + std::lock_guard sync(lock); return i2clib_writeread(handle, deviceAddress, (const char*) dataToSend, (int32_t) sendSize, (char*) dataReceived, (int32_t) receiveSize); } } @@ -1400,7 +1400,7 @@ int32_t i2CWrite(uint8_t port, uint8_t deviceAddress, uint8_t* dataToSend, uint8 int32_t handle = port == 0 ? i2COnBoardHandle:i2CMXPHandle; priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::unique_lock sync(lock); + std::lock_guard sync(lock); return i2clib_write(handle, deviceAddress, (const char*) dataToSend, (int32_t) sendSize); } } @@ -1427,7 +1427,7 @@ int32_t i2CRead(uint8_t port, uint8_t deviceAddress, uint8_t *buffer, uint8_t co int32_t handle = port == 0 ? i2COnBoardHandle:i2CMXPHandle; priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::unique_lock sync(lock); + std::lock_guard sync(lock); return i2clib_read(handle, deviceAddress, (char*) buffer, (int32_t) count); } @@ -1440,7 +1440,7 @@ void i2CClose(uint8_t port) { } priority_recursive_mutex &lock = port == 0 ? digitalI2COnBoardMutex : digitalI2CMXPMutex; { - std::unique_lock 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/lib/Athena/cpp/Resource.cpp b/hal/lib/Athena/cpp/Resource.cpp index 41146ce526..3e7e86d171 100644 --- a/hal/lib/Athena/cpp/Resource.cpp +++ b/hal/lib/Athena/cpp/Resource.cpp @@ -19,7 +19,7 @@ priority_recursive_mutex Resource::m_createLock; * have been allocated yet. The indicies of the resources are [0 .. elements - 1]. */ Resource::Resource(uint32_t elements) { - std::unique_lock sync(m_createLock); + std::lock_guard sync(m_createLock); m_isAllocated = std::vector(elements, false); } @@ -35,7 +35,7 @@ Resource::Resource(uint32_t elements) { */ /*static*/ void Resource::CreateResourceObject(Resource **r, uint32_t elements) { - std::unique_lock sync(m_createLock); + std::lock_guard sync(m_createLock); if (*r == NULL) { *r = new Resource(elements); @@ -49,7 +49,7 @@ Resource::Resource(uint32_t elements) { */ uint32_t Resource::Allocate(const char *resourceDesc) { - std::unique_lock sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); for (uint32_t i=0; i < m_isAllocated.size(); i++) { if (!m_isAllocated[i]) @@ -69,7 +69,7 @@ uint32_t Resource::Allocate(const char *resourceDesc) */ uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc) { - std::unique_lock sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); if (index >= m_isAllocated.size()) { // TODO: wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); @@ -92,7 +92,7 @@ uint32_t Resource::Allocate(uint32_t index, const char *resourceDesc) */ void Resource::Free(uint32_t index) { - std::unique_lock sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); if (index == ~0ul) return; if (index >= m_isAllocated.size()) { diff --git a/hal/lib/Athena/cpp/Semaphore.cpp b/hal/lib/Athena/cpp/Semaphore.cpp index e92eccbccf..458ca6e721 100644 --- a/hal/lib/Athena/cpp/Semaphore.cpp +++ b/hal/lib/Athena/cpp/Semaphore.cpp @@ -11,7 +11,7 @@ Semaphore::Semaphore(uint32_t count) { } void Semaphore::give() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); ++m_count; m_condition.notify_one(); } @@ -23,7 +23,7 @@ void Semaphore::take() { } bool Semaphore::tryTake() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_count) { --m_count; return true; diff --git a/wpilibc/wpilibC++/src/Commands/Scheduler.cpp b/wpilibc/wpilibC++/src/Commands/Scheduler.cpp index 2980f233a0..0b796e3b27 100644 --- a/wpilibc/wpilibC++/src/Commands/Scheduler.cpp +++ b/wpilibc/wpilibC++/src/Commands/Scheduler.cpp @@ -38,7 +38,7 @@ void Scheduler::SetEnabled(bool enabled) { m_enabled = enabled; } * @param command The command to be scheduled */ void Scheduler::AddCommand(Command *command) { - std::unique_lock sync(m_additionsLock); + std::lock_guard sync(m_additionsLock); if (std::find(m_additions.begin(), m_additions.end(), command) != m_additions.end()) return; @@ -46,7 +46,7 @@ void Scheduler::AddCommand(Command *command) { } void Scheduler::AddButton(ButtonScheduler *button) { - std::unique_lock sync(m_buttonsLock); + std::lock_guard sync(m_buttonsLock); m_buttons.push_back(button); } @@ -110,7 +110,7 @@ void Scheduler::Run() { { if (!m_enabled) return; - std::unique_lock sync(m_buttonsLock); + std::lock_guard sync(m_buttonsLock); auto rButtonIter = m_buttons.rbegin(); for (; rButtonIter != m_buttons.rend(); rButtonIter++) { (*rButtonIter)->Execute(); @@ -133,7 +133,7 @@ void Scheduler::Run() { // Add the new things { - std::unique_lock sync(m_additionsLock); + std::lock_guard sync(m_additionsLock); auto additionsIter = m_additions.begin(); for (; additionsIter != m_additions.end(); additionsIter++) { ProcessCommandAddition(*additionsIter); diff --git a/wpilibc/wpilibC++/src/ErrorBase.cpp b/wpilibc/wpilibC++/src/ErrorBase.cpp index aab413a9d7..ecde2a04ad 100644 --- a/wpilibc/wpilibC++/src/ErrorBase.cpp +++ b/wpilibc/wpilibC++/src/ErrorBase.cpp @@ -58,7 +58,7 @@ void ErrorBase::SetErrnoError(const std::string& contextMessage, m_error.Set(-1, err, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -87,7 +87,7 @@ void ErrorBase::SetImaqError(int success, const std::string& contextMessage, m_error.Set(success, err.str(), filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -113,7 +113,7 @@ void ErrorBase::SetError(Error::Code code, const std::string& contextMessage, m_error.Set(code, contextMessage, filename, function, lineNumber, this); // Update the global error if there is not one already set. - std::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -140,7 +140,7 @@ void ErrorBase::SetWPIError(const std::string& 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::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() == 0) { _globalError.Clone(m_error); } @@ -164,7 +164,7 @@ void ErrorBase::SetGlobalError(Error::Code code, uint32_t lineNumber) { // If there was an error if (code != 0) { - std::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); // Set the current error information for this object. _globalError.Set(code, contextMessage, filename, function, lineNumber, @@ -179,7 +179,7 @@ void ErrorBase::SetGlobalWPIError(const std::string& errorMessage, uint32_t lineNumber) { std::string err = errorMessage + ": " + contextMessage; - std::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); if (_globalError.GetCode() != 0) { _globalError.Clear(); } @@ -190,6 +190,6 @@ void ErrorBase::SetGlobalWPIError(const std::string& errorMessage, * Retrieve the current global error. */ Error& ErrorBase::GetGlobalError() { - std::unique_lock mutex(_globalErrorMutex); + std::lock_guard mutex(_globalErrorMutex); return _globalError; } diff --git a/wpilibc/wpilibC++/src/Resource.cpp b/wpilibc/wpilibC++/src/Resource.cpp index d155e28dfd..7f9e476cf1 100644 --- a/wpilibc/wpilibC++/src/Resource.cpp +++ b/wpilibc/wpilibC++/src/Resource.cpp @@ -19,7 +19,7 @@ priority_recursive_mutex Resource::m_createLock; * 1]. */ Resource::Resource(uint32_t elements) { - std::unique_lock sync(m_createLock); + std::lock_guard sync(m_createLock); m_isAllocated = std::vector(elements, false); } @@ -35,7 +35,7 @@ Resource::Resource(uint32_t elements) { */ /*static*/ void Resource::CreateResourceObject(std::unique_ptr& r, uint32_t elements) { - std::unique_lock sync(m_createLock); + std::lock_guard sync(m_createLock); if (!r) { r = std::make_unique(elements); } @@ -48,7 +48,7 @@ Resource::Resource(uint32_t elements) { * within the range is located and returned after it is marked allocated. */ uint32_t Resource::Allocate(const std::string &resourceDesc) { - std::unique_lock sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); for (uint32_t i = 0; i < m_isAllocated.size(); i++) { if (!m_isAllocated[i]) { m_isAllocated[i] = true; @@ -66,7 +66,7 @@ uint32_t Resource::Allocate(const std::string &resourceDesc) { * unallocated, then returned. */ uint32_t Resource::Allocate(uint32_t index, const std::string &resourceDesc) { - std::unique_lock sync(m_allocateLock); + std::lock_guard sync(m_allocateLock); if (index >= m_isAllocated.size()) { wpi_setWPIErrorWithContext(ChannelIndexOutOfRange, resourceDesc); return std::numeric_limits::max(); diff --git a/wpilibc/wpilibC++Devices/src/CameraServer.cpp b/wpilibc/wpilibC++Devices/src/CameraServer.cpp index fad86586f4..8565edad7b 100644 --- a/wpilibc/wpilibC++Devices/src/CameraServer.cpp +++ b/wpilibc/wpilibC++Devices/src/CameraServer.cpp @@ -35,14 +35,14 @@ void CameraServer::FreeImageData( if (std::get<3>(imageData)) imaqDispose(std::get<0>(imageData)); else if (std::get<0>(imageData) != nullptr) { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); m_dataPool.push_back(std::get<0>(imageData)); } } void CameraServer::SetImageData(uint8_t* data, unsigned int size, unsigned int start, bool imaqData) { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); FreeImageData(m_imageData); m_imageData = std::make_tuple(data, size, start, imaqData); m_newImageVariable.notify_all(); @@ -58,7 +58,7 @@ void CameraServer::SetImage(Image const* image) { bool hwClient; { // Make a local copy of the hwClient variable so that we can safely use it. - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); hwClient = m_hwClient; } unsigned int start = 0; @@ -83,7 +83,7 @@ void CameraServer::AutoCapture() { bool hwClient; uint8_t* data = nullptr; { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); hwClient = m_hwClient; if (hwClient) { data = m_dataPool.back(); @@ -109,7 +109,7 @@ void CameraServer::StartAutomaticCapture(char const* cameraName) { } void CameraServer::StartAutomaticCapture(std::shared_ptr camera) { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); if (m_autoCaptureStarted) return; m_camera = camera; @@ -121,12 +121,12 @@ void CameraServer::StartAutomaticCapture(std::shared_ptr camera) { } bool CameraServer::IsAutoCaptureStarted() { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); return m_autoCaptureStarted; } void CameraServer::SetSize(unsigned int size) { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); if (!m_camera) return; if (size == kSize160x120) m_camera->SetSize(160, 120); @@ -137,12 +137,12 @@ void CameraServer::SetSize(unsigned int size) { } void CameraServer::SetQuality(unsigned int quality) { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); m_quality = quality > 100 ? 100 : quality; } unsigned int CameraServer::GetQuality() { - std::unique_lock lock(m_imageMutex); + std::lock_guard lock(m_imageMutex); return m_quality; } diff --git a/wpilibc/wpilibC++Devices/src/MotorSafetyHelper.cpp b/wpilibc/wpilibC++Devices/src/MotorSafetyHelper.cpp index e4fb035fd3..60bba3068b 100644 --- a/wpilibc/wpilibC++Devices/src/MotorSafetyHelper.cpp +++ b/wpilibc/wpilibC++Devices/src/MotorSafetyHelper.cpp @@ -37,12 +37,12 @@ MotorSafetyHelper::MotorSafetyHelper(MotorSafety *safeObject) m_expiration = DEFAULT_SAFETY_EXPIRATION; m_stopTime = Timer::GetFPGATimestamp(); - std::unique_lock sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.insert(this); } MotorSafetyHelper::~MotorSafetyHelper() { - std::unique_lock sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.erase(this); } @@ -51,7 +51,7 @@ MotorSafetyHelper::~MotorSafetyHelper() { * Resets the timer on this object that is used to do the timeouts. */ void MotorSafetyHelper::Feed() { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_stopTime = Timer::GetFPGATimestamp() + m_expiration; } @@ -60,7 +60,7 @@ void MotorSafetyHelper::Feed() { * @param expirationTime The timeout value in seconds. */ void MotorSafetyHelper::SetExpiration(float expirationTime) { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_expiration = expirationTime; } @@ -69,7 +69,7 @@ void MotorSafetyHelper::SetExpiration(float expirationTime) { * @return the timeout value in seconds. */ float MotorSafetyHelper::GetExpiration() const { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_expiration; } @@ -79,7 +79,7 @@ float MotorSafetyHelper::GetExpiration() const { * timed out. */ bool MotorSafetyHelper::IsAlive() const { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return !m_enabled || m_stopTime > Timer::GetFPGATimestamp(); } @@ -95,7 +95,7 @@ void MotorSafetyHelper::Check() { DriverStation &ds = DriverStation::GetInstance(); if (!m_enabled || ds.IsDisabled() || ds.IsTest()) return; - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); if (m_stopTime < Timer::GetFPGATimestamp()) { std::ostringstream desc; m_safeObject->GetDescription(desc); @@ -111,7 +111,7 @@ void MotorSafetyHelper::Check() { * @param enabled True if motor safety is enforced for this object */ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { - std::unique_lock 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::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_enabled; } @@ -132,7 +132,7 @@ bool MotorSafetyHelper::IsSafetyEnabled() const { * timed out. */ void MotorSafetyHelper::CheckMotors() { - std::unique_lock sync(m_listMutex); + std::lock_guard sync(m_listMutex); for (auto elem : m_helperList) { elem->Check(); } diff --git a/wpilibc/wpilibC++Devices/src/Notifier.cpp b/wpilibc/wpilibC++Devices/src/Notifier.cpp index 19016107c7..02a1a47d80 100644 --- a/wpilibc/wpilibC++Devices/src/Notifier.cpp +++ b/wpilibc/wpilibC++Devices/src/Notifier.cpp @@ -27,7 +27,7 @@ Notifier::Notifier(TimerEventHandler handler, void *param) { m_handler = handler; m_param = param; { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); // do the first time intialization of static variables if (refcount == 0) { int32_t status = 0; @@ -45,7 +45,7 @@ Notifier::Notifier(TimerEventHandler handler, void *param) { */ Notifier::~Notifier() { { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); DeleteFromQueue(); // Delete the static variables when the last one is going away @@ -58,7 +58,7 @@ Notifier::~Notifier() { // Acquire the mutex; this makes certain that the handler is // not being executed by the interrupt manager. - std::unique_lock lock(m_handlerMutex); + std::lock_guard lock(m_handlerMutex); } /** @@ -93,7 +93,7 @@ void Notifier::ProcessQueue(uint32_t mask, void *params) { while (true) // keep processing past events until no more { { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); double currentTime = GetClock(); current = timerQueueHead; if (current == nullptr || current->m_expirationTime > currentTime) { @@ -118,7 +118,7 @@ void Notifier::ProcessQueue(uint32_t mask, void *params) { current->m_handlerMutex.unlock(); } // reschedule the first item in the queue - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); UpdateAlarm(); } @@ -203,7 +203,7 @@ void Notifier::DeleteFromQueue() { * @param delay Seconds to wait before the handler is called. */ void Notifier::StartSingle(double delay) { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); m_periodic = false; m_period = delay; DeleteFromQueue(); @@ -219,7 +219,7 @@ void Notifier::StartSingle(double delay) { * the call to this method. */ void Notifier::StartPeriodic(double period) { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); m_periodic = true; m_period = period; DeleteFromQueue(); @@ -237,10 +237,10 @@ void Notifier::StartPeriodic(double period) { */ void Notifier::Stop() { { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); DeleteFromQueue(); } // Wait for a currently executing handler to complete before returning from // Stop() - std::unique_lock sync(m_handlerMutex); + std::lock_guard sync(m_handlerMutex); } diff --git a/wpilibc/wpilibC++Devices/src/PIDController.cpp b/wpilibc/wpilibC++Devices/src/PIDController.cpp index e9a25f752a..ed73b54700 100644 --- a/wpilibc/wpilibC++Devices/src/PIDController.cpp +++ b/wpilibc/wpilibC++Devices/src/PIDController.cpp @@ -103,7 +103,7 @@ void PIDController::Calculate() { PIDOutput *pidOutput; { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); pidInput = m_pidInput; pidOutput = m_pidOutput; enabled = m_enabled; @@ -114,74 +114,72 @@ void PIDController::Calculate() { if (pidOutput == nullptr) return; if (enabled) { - { - std::unique_lock sync(m_mutex); - float input = pidInput->PIDGet(); - float result; - PIDOutput *pidOutput; + std::lock_guard sync(m_mutex); + float input = pidInput->PIDGet(); + float result; + PIDOutput *pidOutput; - m_error = m_setpoint - input; - if (m_continuous) { - if (fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { - if (m_error > 0) { - m_error = m_error - m_maximumInput + m_minimumInput; - } else { - m_error = m_error + m_maximumInput - m_minimumInput; - } + m_error = m_setpoint - input; + if (m_continuous) { + if (fabs(m_error) > (m_maximumInput - m_minimumInput) / 2) { + if (m_error > 0) { + m_error = m_error - m_maximumInput + m_minimumInput; + } else { + m_error = m_error + m_maximumInput - m_minimumInput; + } + } + } + + if (m_pidInput->GetPIDSourceType() == PIDSourceType::kRate) { + if (m_P != 0) { + double potentialPGain = (m_totalError + m_error) * m_P; + if (potentialPGain < m_maximumOutput) { + if (potentialPGain > m_minimumOutput) + m_totalError += m_error; + else + m_totalError = m_minimumOutput / m_P; + } else { + m_totalError = m_maximumOutput / m_P; } } - if (m_pidInput->GetPIDSourceType() == PIDSourceType::kRate) { - if (m_P != 0) { - double potentialPGain = (m_totalError + m_error) * m_P; - if (potentialPGain < m_maximumOutput) { - if (potentialPGain > m_minimumOutput) - m_totalError += m_error; - else - m_totalError = m_minimumOutput / m_P; - } else { - m_totalError = m_maximumOutput / m_P; - } + m_result = m_D * m_error + m_P * m_totalError + m_setpoint * m_F; + } + else { + if (m_I != 0) { + double potentialIGain = (m_totalError + m_error) * m_I; + if (potentialIGain < m_maximumOutput) { + if (potentialIGain > m_minimumOutput) + m_totalError += m_error; + else + m_totalError = m_minimumOutput / m_I; + } else { + m_totalError = m_maximumOutput / m_I; } - - m_result = m_D * m_error + m_P * m_totalError + m_setpoint * m_F; } - else { - if (m_I != 0) { - double potentialIGain = (m_totalError + m_error) * m_I; - if (potentialIGain < m_maximumOutput) { - if (potentialIGain > m_minimumOutput) - m_totalError += m_error; - else - m_totalError = m_minimumOutput / m_I; - } else { - m_totalError = m_maximumOutput / m_I; - } - } - m_result = m_P * m_error + m_I * m_totalError + - m_D * (m_prevInput - input) + m_setpoint * m_F; - } - m_prevInput = input; + m_result = m_P * m_error + m_I * m_totalError + + m_D * (m_prevInput - input) + m_setpoint * m_F; + } + m_prevInput = input; - if (m_result > m_maximumOutput) - m_result = m_maximumOutput; - else if (m_result < m_minimumOutput) - m_result = m_minimumOutput; + if (m_result > m_maximumOutput) + m_result = m_maximumOutput; + else if (m_result < m_minimumOutput) + m_result = m_minimumOutput; - pidOutput = m_pidOutput; - result = m_result; + pidOutput = m_pidOutput; + result = m_result; - pidOutput->PIDWrite(result); + pidOutput->PIDWrite(result); - // Update the buffer. - m_buf.push(m_error); - m_bufTotal += m_error; - // Remove old elements when buffer is full. - if (m_buf.size() > m_bufLength) { - m_bufTotal -= m_buf.front(); - m_buf.pop(); - } + // Update the buffer. + m_buf.push(m_error); + m_bufTotal += m_error; + // Remove old elements when buffer is full. + if (m_buf.size() > m_bufLength) { + m_bufTotal -= m_buf.front(); + m_buf.pop(); } } } @@ -195,7 +193,7 @@ void PIDController::Calculate() { */ void PIDController::SetPID(double p, double i, double d) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_P = p; m_I = i; m_D = d; @@ -218,7 +216,7 @@ void PIDController::SetPID(double p, double i, double d) { */ void PIDController::SetPID(double p, double i, double d, double f) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_P = p; m_I = i; m_D = d; @@ -238,7 +236,7 @@ void PIDController::SetPID(double p, double i, double d, double f) { * @return proportional coefficient */ double PIDController::GetP() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_P; } @@ -247,7 +245,7 @@ double PIDController::GetP() const { * @return integral coefficient */ double PIDController::GetI() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_I; } @@ -256,7 +254,7 @@ double PIDController::GetI() const { * @return differential coefficient */ double PIDController::GetD() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_D; } @@ -265,7 +263,7 @@ double PIDController::GetD() const { * @return Feed forward coefficient */ double PIDController::GetF() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_F; } @@ -275,7 +273,7 @@ double PIDController::GetF() const { * @return the latest calculated output */ float PIDController::Get() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_result; } @@ -287,7 +285,7 @@ float PIDController::Get() const { * @param continuous Set to true turns on continuous, false turns off continuous */ void PIDController::SetContinuous(bool continuous) { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_continuous = continuous; } @@ -299,7 +297,7 @@ void PIDController::SetContinuous(bool continuous) { */ void PIDController::SetInputRange(float minimumInput, float maximumInput) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_minimumInput = minimumInput; m_maximumInput = maximumInput; } @@ -315,7 +313,7 @@ void PIDController::SetInputRange(float minimumInput, float maximumInput) { */ void PIDController::SetOutputRange(float minimumOutput, float maximumOutput) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_minimumOutput = minimumOutput; m_maximumOutput = maximumOutput; } @@ -328,7 +326,7 @@ void PIDController::SetOutputRange(float minimumOutput, float maximumOutput) { */ void PIDController::SetSetpoint(float setpoint) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_maximumInput > m_minimumInput) { if (setpoint > m_maximumInput) m_setpoint = m_maximumInput; @@ -354,7 +352,7 @@ void PIDController::SetSetpoint(float setpoint) { * @return the current setpoint */ double PIDController::GetSetpoint() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_setpoint; } @@ -365,7 +363,7 @@ double PIDController::GetSetpoint() const { float PIDController::GetError() const { double pidInput; { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); pidInput = m_pidInput->PIDGet(); } return GetSetpoint() - pidInput; @@ -394,7 +392,7 @@ PIDSourceType PIDController::GetPIDSourceType() const { float PIDController::GetAvgError() const { float avgError = 0; { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); // Don't divide by zero. if (m_buf.size()) avgError = m_bufTotal / m_buf.size(); } @@ -408,7 +406,7 @@ float PIDController::GetAvgError() const { */ void PIDController::SetTolerance(float percent) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -421,7 +419,7 @@ void PIDController::SetTolerance(float percent) { */ void PIDController::SetPercentTolerance(float percent) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -434,7 +432,7 @@ void PIDController::SetPercentTolerance(float percent) { */ void PIDController::SetAbsoluteTolerance(float absTolerance) { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_toleranceType = kAbsoluteTolerance; m_tolerance = absTolerance; } @@ -471,7 +469,7 @@ void PIDController::SetToleranceBuffer(unsigned bufLength) { bool PIDController::OnTarget() const { double error = GetAvgError(); - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); switch (m_toleranceType) { case kPercentTolerance: return fabs(error) < m_tolerance / 100 * (m_maximumInput - m_minimumInput); @@ -491,7 +489,7 @@ bool PIDController::OnTarget() const { */ void PIDController::Enable() { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_enabled = true; } @@ -505,7 +503,7 @@ void PIDController::Enable() { */ void PIDController::Disable() { { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_pidOutput->PIDWrite(0); m_enabled = false; } @@ -519,7 +517,7 @@ void PIDController::Disable() { * Return true if PIDController is enabled. */ bool PIDController::IsEnabled() const { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); return m_enabled; } @@ -529,7 +527,7 @@ bool PIDController::IsEnabled() const { void PIDController::Reset() { Disable(); - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_prevInput = 0; m_totalError = 0; m_result = 0; diff --git a/wpilibc/wpilibC++Devices/src/Timer.cpp b/wpilibc/wpilibC++Devices/src/Timer.cpp index 9486db1328..53631e778f 100644 --- a/wpilibc/wpilibC++Devices/src/Timer.cpp +++ b/wpilibc/wpilibC++Devices/src/Timer.cpp @@ -79,7 +79,7 @@ double Timer::Get() const { double result; double currentTime = GetFPGATimestamp(); - std::unique_lock 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 @@ -103,7 +103,7 @@ double Timer::Get() const { * now */ void Timer::Reset() { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_accumulatedTime = 0; m_startTime = GetFPGATimestamp(); } @@ -114,7 +114,7 @@ void Timer::Reset() { * relative to the system clock. */ void Timer::Start() { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); if (!m_running) { m_startTime = GetFPGATimestamp(); m_running = true; @@ -130,7 +130,7 @@ void Timer::Start() { void Timer::Stop() { double temp = Get(); - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_running) { m_accumulatedTime = temp; m_running = false; @@ -147,7 +147,7 @@ void Timer::Stop() { */ bool Timer::HasPeriodPassed(double period) { if (Get() > period) { - std::unique_lock 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/wpilibC++Devices/src/USBCamera.cpp b/wpilibc/wpilibC++Devices/src/USBCamera.cpp index eb61eca9b4..0c57d85e95 100644 --- a/wpilibc/wpilibC++Devices/src/USBCamera.cpp +++ b/wpilibc/wpilibC++Devices/src/USBCamera.cpp @@ -77,7 +77,7 @@ USBCamera::USBCamera(std::string name, bool useJpeg) m_useJpeg(useJpeg) {} void USBCamera::OpenCamera() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); for (unsigned int i = 0; i < 3; i++) { uInt32 id = 0; // Can't use SAFE_IMAQ_CALL here because we only error on the third time @@ -97,7 +97,7 @@ void USBCamera::OpenCamera() { } void USBCamera::CloseCamera() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (!m_open) return; SAFE_IMAQ_CALL(IMAQdxCloseCamera, m_id); m_id = 0; @@ -105,7 +105,7 @@ void USBCamera::CloseCamera() { } void USBCamera::StartCapture() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (!m_open || m_active) return; SAFE_IMAQ_CALL(IMAQdxConfigureGrab, m_id); SAFE_IMAQ_CALL(IMAQdxStartAcquisition, m_id); @@ -113,7 +113,7 @@ void USBCamera::StartCapture() { } void USBCamera::StopCapture() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (!m_open || !m_active) return; SAFE_IMAQ_CALL(IMAQdxStopAcquisition, m_id); SAFE_IMAQ_CALL(IMAQdxUnconfigureAcquisition, m_id); @@ -121,7 +121,7 @@ void USBCamera::StopCapture() { } void USBCamera::UpdateSettings() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); bool wasActive = m_active; if (wasActive) StopCapture(); @@ -215,7 +215,7 @@ void USBCamera::UpdateSettings() { } void USBCamera::SetFPS(double fps) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_fps != fps) { m_needSettingsUpdate = true; m_fps = fps; @@ -223,7 +223,7 @@ void USBCamera::SetFPS(double fps) { } void USBCamera::SetSize(unsigned int width, unsigned int height) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_width != width || m_height != height) { m_needSettingsUpdate = true; m_width = width; @@ -232,7 +232,7 @@ void USBCamera::SetSize(unsigned int width, unsigned int height) { } void USBCamera::SetBrightness(unsigned int brightness) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_brightness != brightness) { m_needSettingsUpdate = true; m_brightness = brightness; @@ -240,12 +240,12 @@ void USBCamera::SetBrightness(unsigned int brightness) { } unsigned int USBCamera::GetBrightness() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_brightness; } void USBCamera::SetWhiteBalanceAuto() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_whiteBalance = AUTO; m_whiteBalanceValue = 0; m_whiteBalanceValuePresent = false; @@ -253,7 +253,7 @@ void USBCamera::SetWhiteBalanceAuto() { } void USBCamera::SetWhiteBalanceHoldCurrent() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_whiteBalance = MANUAL; m_whiteBalanceValue = 0; m_whiteBalanceValuePresent = false; @@ -261,7 +261,7 @@ void USBCamera::SetWhiteBalanceHoldCurrent() { } void USBCamera::SetWhiteBalanceManual(unsigned int whiteBalance) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_whiteBalance = MANUAL; m_whiteBalanceValue = whiteBalance; m_whiteBalanceValuePresent = true; @@ -269,7 +269,7 @@ void USBCamera::SetWhiteBalanceManual(unsigned int whiteBalance) { } void USBCamera::SetExposureAuto() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_exposure = AUTO; m_exposureValue = 0; m_exposureValuePresent = false; @@ -277,7 +277,7 @@ void USBCamera::SetExposureAuto() { } void USBCamera::SetExposureHoldCurrent() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_exposure = MANUAL; m_exposureValue = 0; m_exposureValuePresent = false; @@ -285,7 +285,7 @@ void USBCamera::SetExposureHoldCurrent() { } void USBCamera::SetExposureManual(unsigned int level) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_exposure = MANUAL; if (level > 100) m_exposureValue = 100; @@ -296,7 +296,7 @@ void USBCamera::SetExposureManual(unsigned int level) { } void USBCamera::GetImage(Image* image) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_needSettingsUpdate || m_useJpeg) { m_needSettingsUpdate = false; m_useJpeg = false; @@ -309,7 +309,7 @@ void USBCamera::GetImage(Image* image) { } unsigned int USBCamera::GetImageData(void* buffer, unsigned int bufferSize) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_needSettingsUpdate || !m_useJpeg) { m_needSettingsUpdate = false; m_useJpeg = true; diff --git a/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp b/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp index c7bfa24934..c2a53f3fd5 100644 --- a/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp +++ b/wpilibc/wpilibC++Devices/src/Ultrasonic.cpp @@ -67,7 +67,7 @@ void Ultrasonic::Initialize() { SetAutomaticMode(false); // kill task when adding a new sensor // link this instance on the list { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_nextSensor = m_firstSensor; m_firstSensor = this; } @@ -188,7 +188,7 @@ Ultrasonic::~Ultrasonic() { wpi_assert(m_firstSensor != nullptr); { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (this == m_firstSensor) { m_firstSensor = m_nextSensor; if (m_firstSensor == nullptr) { diff --git a/wpilibc/wpilibC++IntegrationTests/src/MutexTest.cpp b/wpilibc/wpilibC++IntegrationTests/src/MutexTest.cpp index ad3ecbbbb5..1e80ec9aa1 100644 --- a/wpilibc/wpilibC++IntegrationTests/src/MutexTest.cpp +++ b/wpilibc/wpilibC++IntegrationTests/src/MutexTest.cpp @@ -24,7 +24,7 @@ class Notification { } // Sets the condition to true, and wakes all waiting threads. void Notify() { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_set = true; m_condition.notify_all(); } diff --git a/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp b/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp index fa1b7096e9..87f5352169 100644 --- a/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp +++ b/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp @@ -37,12 +37,12 @@ MotorSafetyHelper::MotorSafetyHelper(MotorSafety *safeObject) m_expiration = DEFAULT_SAFETY_EXPIRATION; m_stopTime = Timer::GetFPGATimestamp(); - std::unique_lock sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.insert(this); } MotorSafetyHelper::~MotorSafetyHelper() { - std::unique_lock sync(m_listMutex); + std::lock_guard sync(m_listMutex); m_helperList.erase(this); } @@ -51,7 +51,7 @@ MotorSafetyHelper::~MotorSafetyHelper() { * Resets the timer on this object that is used to do the timeouts. */ void MotorSafetyHelper::Feed() { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_stopTime = Timer::GetFPGATimestamp() + m_expiration; } @@ -60,7 +60,7 @@ void MotorSafetyHelper::Feed() { * @param expirationTime The timeout value in seconds. */ void MotorSafetyHelper::SetExpiration(float expirationTime) { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_expiration = expirationTime; } @@ -69,7 +69,7 @@ void MotorSafetyHelper::SetExpiration(float expirationTime) { * @return the timeout value in seconds. */ float MotorSafetyHelper::GetExpiration() const { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_expiration; } @@ -79,7 +79,7 @@ float MotorSafetyHelper::GetExpiration() const { * timed out. */ bool MotorSafetyHelper::IsAlive() const { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return !m_enabled || m_stopTime > Timer::GetFPGATimestamp(); } @@ -96,7 +96,7 @@ void MotorSafetyHelper::Check() DriverStation &ds = DriverStation::GetInstance(); if (!m_enabled || ds.IsDisabled() || ds.IsTest()) return; - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); if (m_stopTime < Timer::GetFPGATimestamp()) { std::ostringstream desc; m_safeObject->GetDescription(desc); @@ -112,7 +112,7 @@ void MotorSafetyHelper::Check() * @param enabled True if motor safety is enforced for this object */ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); m_enabled = enabled; } @@ -122,7 +122,7 @@ void MotorSafetyHelper::SetSafetyEnabled(bool enabled) { * @return True if motor safety is enforced for this device */ bool MotorSafetyHelper::IsSafetyEnabled() const { - std::unique_lock sync(m_syncMutex); + std::lock_guard sync(m_syncMutex); return m_enabled; } @@ -133,7 +133,7 @@ bool MotorSafetyHelper::IsSafetyEnabled() const { * timed out. */ void MotorSafetyHelper::CheckMotors() { - std::unique_lock sync(m_listMutex); + std::lock_guard sync(m_listMutex); for (auto elem : m_helperList) { elem->Check(); } diff --git a/wpilibc/wpilibC++Sim/src/Notifier.cpp b/wpilibc/wpilibC++Sim/src/Notifier.cpp index 00856fe275..8442f6b4b3 100644 --- a/wpilibc/wpilibC++Sim/src/Notifier.cpp +++ b/wpilibc/wpilibC++Sim/src/Notifier.cpp @@ -32,7 +32,7 @@ Notifier::Notifier(TimerEventHandler handler, void *param) m_nextEvent = nullptr; m_queued = false; { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); // do the first time intialization of static variables if (refcount == 0) { @@ -50,7 +50,7 @@ Notifier::Notifier(TimerEventHandler handler, void *param) Notifier::~Notifier() { { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); DeleteFromQueue(); // Delete the static variables when the last one is going away @@ -63,7 +63,7 @@ Notifier::~Notifier() // Acquire the semaphore; this makes certain that the handler is // not being executed by the interrupt manager. - std::unique_lock lock(m_handlerMutex); + std::lock_guard lock(m_handlerMutex); } /** @@ -89,7 +89,7 @@ void Notifier::ProcessQueue(uint32_t mask, void *params) while (true) // keep processing past events until no more { { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); double currentTime = GetClock(); current = timerQueueHead; if (current == nullptr || current->m_expirationTime > currentTime) @@ -118,7 +118,7 @@ void Notifier::ProcessQueue(uint32_t mask, void *params) current->m_handlerMutex.unlock(); } // reschedule the first item in the queue - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); UpdateAlarm(); } @@ -209,7 +209,7 @@ void Notifier::DeleteFromQueue() */ void Notifier::StartSingle(double delay) { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); m_periodic = false; m_period = delay; DeleteFromQueue(); @@ -224,7 +224,7 @@ void Notifier::StartSingle(double delay) */ void Notifier::StartPeriodic(double period) { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); m_periodic = true; m_period = period; DeleteFromQueue(); @@ -241,11 +241,11 @@ void Notifier::StartPeriodic(double period) void Notifier::Stop() { { - std::unique_lock sync(queueMutex); + std::lock_guard sync(queueMutex); DeleteFromQueue(); } // Wait for a currently executing handler to complete before returning from Stop() - std::unique_lock sync(m_handlerMutex); + std::lock_guard sync(m_handlerMutex); } void Notifier::Run() { diff --git a/wpilibc/wpilibC++Sim/src/PIDController.cpp b/wpilibc/wpilibC++Sim/src/PIDController.cpp index cd2a5e4073..03beb5c119 100644 --- a/wpilibc/wpilibC++Sim/src/PIDController.cpp +++ b/wpilibc/wpilibC++Sim/src/PIDController.cpp @@ -121,7 +121,7 @@ void PIDController::Calculate() PIDSource *pidInput; { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_pidInput == 0) return; if (m_pidOutput == 0) return; enabled = m_enabled; @@ -135,7 +135,7 @@ void PIDController::Calculate() PIDOutput *pidOutput; { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_error = m_setpoint - input; if (m_continuous) { @@ -211,7 +211,7 @@ void PIDController::Calculate() void PIDController::SetPID(double p, double i, double d) { { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_P = p; m_I = i; m_D = d; @@ -235,7 +235,7 @@ void PIDController::SetPID(double p, double i, double d) void PIDController::SetPID(double p, double i, double d, double f) { { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_P = p; m_I = i; m_D = d; @@ -256,7 +256,7 @@ void PIDController::SetPID(double p, double i, double d, double f) */ double PIDController::GetP() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_P; } @@ -266,7 +266,7 @@ double PIDController::GetP() const */ double PIDController::GetI() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_I; } @@ -276,7 +276,7 @@ double PIDController::GetI() const */ double PIDController::GetD() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_D; } @@ -286,7 +286,7 @@ double PIDController::GetD() const */ double PIDController::GetF() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_F; } @@ -297,7 +297,7 @@ double PIDController::GetF() const */ float PIDController::Get() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_result; } @@ -310,7 +310,7 @@ float PIDController::Get() const */ void PIDController::SetContinuous(bool continuous) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_continuous = continuous; } @@ -323,7 +323,7 @@ void PIDController::SetContinuous(bool continuous) void PIDController::SetInputRange(float minimumInput, float maximumInput) { { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_minimumInput = minimumInput; m_maximumInput = maximumInput; } @@ -339,7 +339,7 @@ void PIDController::SetInputRange(float minimumInput, float maximumInput) */ void PIDController::SetOutputRange(float minimumOutput, float maximumOutput) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_minimumOutput = minimumOutput; m_maximumOutput = maximumOutput; } @@ -351,7 +351,7 @@ void PIDController::SetOutputRange(float minimumOutput, float maximumOutput) void PIDController::SetSetpoint(float setpoint) { { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); if (m_maximumInput > m_minimumInput) { if (setpoint > m_maximumInput) @@ -378,7 +378,7 @@ void PIDController::SetSetpoint(float setpoint) */ double PIDController::GetSetpoint() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_setpoint; } @@ -390,7 +390,7 @@ float PIDController::GetError() const { double pidInput; { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); pidInput = m_pidInput->PIDGet(); } return GetSetpoint() - pidInput; @@ -420,7 +420,7 @@ PIDSourceType PIDController::GetPIDSourceType() const { float PIDController::GetAvgError() const { float avgError = 0; { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); // Don't divide by zero. if (m_buf.size()) avgError = m_bufTotal / m_buf.size(); } @@ -434,7 +434,7 @@ float PIDController::GetAvgError() const { */ void PIDController::SetTolerance(float percent) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -446,7 +446,7 @@ void PIDController::SetTolerance(float percent) */ void PIDController::SetPercentTolerance(float percent) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_toleranceType = kPercentTolerance; m_tolerance = percent; } @@ -458,7 +458,7 @@ void PIDController::SetPercentTolerance(float percent) */ void PIDController::SetAbsoluteTolerance(float absTolerance) { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_toleranceType = kAbsoluteTolerance; m_tolerance = absTolerance; } @@ -493,7 +493,7 @@ bool PIDController::OnTarget() const { double error = GetError(); - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); switch (m_toleranceType) { case kPercentTolerance: return fabs(error) < m_tolerance / 100 * (m_maximumInput - m_minimumInput); @@ -513,7 +513,7 @@ bool PIDController::OnTarget() const void PIDController::Enable() { { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_enabled = true; } @@ -528,7 +528,7 @@ void PIDController::Enable() void PIDController::Disable() { { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_pidOutput->PIDWrite(0); m_enabled = false; } @@ -543,7 +543,7 @@ void PIDController::Disable() */ bool PIDController::IsEnabled() const { - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); return m_enabled; } @@ -554,7 +554,7 @@ void PIDController::Reset() { Disable(); - std::unique_lock lock(m_mutex); + std::lock_guard lock(m_mutex); m_prevInput = 0; m_totalError = 0; m_result = 0; diff --git a/wpilibc/wpilibC++Sim/src/Timer.cpp b/wpilibc/wpilibC++Sim/src/Timer.cpp index bf0e7b83cb..b4734dbb89 100644 --- a/wpilibc/wpilibC++Sim/src/Timer.cpp +++ b/wpilibc/wpilibC++Sim/src/Timer.cpp @@ -96,7 +96,7 @@ double Timer::Get() const double result; double currentTime = GetFPGATimestamp(); - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); if(m_running) { // This math won't work if the timer rolled over (71 minutes after boot). @@ -118,7 +118,7 @@ double Timer::Get() const */ void Timer::Reset() { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); m_accumulatedTime = 0; m_startTime = GetFPGATimestamp(); } @@ -130,7 +130,7 @@ void Timer::Reset() */ void Timer::Start() { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); if (!m_running) { m_startTime = GetFPGATimestamp(); @@ -148,7 +148,7 @@ void Timer::Stop() { double temp = Get(); - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); if (m_running) { m_accumulatedTime = temp; @@ -168,7 +168,7 @@ bool Timer::HasPeriodPassed(double period) { if (Get() > period) { - std::unique_lock sync(m_mutex); + std::lock_guard sync(m_mutex); // Advance the start time by the period. // Don't set it to the current time... we want to avoid drift. m_startTime += period;