Replace std::lock_guard and std::lock with std::scoped_lock (#1758)

std::scoped_lock was introduced in C++17 and is strictly better than
std::lock_guard as it supports locking any number of mutexes safely.
It's also easier to use than std::lock for locking multiple mutexes at
once.
This commit is contained in:
Tyler Veness
2019-07-08 22:58:39 -07:00
committed by Peter Johnson
parent 24d31df55a
commit 62be0392b6
79 changed files with 472 additions and 476 deletions

View File

@@ -37,7 +37,7 @@ HttpCameraImpl::~HttpCameraImpl() {
// Close file if it's open
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
if (m_streamConn) m_streamConn->stream->close();
if (m_settingsConn) m_settingsConn->stream->close();
}
@@ -131,7 +131,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
// Build the request
wpi::HttpRequest req;
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
if (m_locations.empty()) {
SERROR("locations array is empty!?");
std::this_thread::sleep_for(std::chrono::seconds(1));
@@ -153,7 +153,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
// update m_streamConn
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_frameCount = 1; // avoid a race with monitor thread
m_streamConn = std::move(connPtr);
}
@@ -161,7 +161,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
std::string warn;
if (!conn->Handshake(req, &warn)) {
SWARNING(GetName() << ": " << warn);
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_streamConn = nullptr;
return nullptr;
}
@@ -173,7 +173,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
if (mediaType != "multipart/x-mixed-replace") {
SWARNING("\"" << req.host << "\": unrecognized Content-Type \"" << mediaType
<< "\"");
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_streamConn = nullptr;
return nullptr;
}
@@ -198,7 +198,7 @@ wpi::HttpConnection* HttpCameraImpl::DeviceStreamConnect(
if (boundary.empty()) {
SWARNING("\"" << req.host
<< "\": empty multi-part boundary or no Content-Type");
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_streamConn = nullptr;
return nullptr;
}
@@ -326,7 +326,7 @@ void HttpCameraImpl::DeviceSendSettings(wpi::HttpRequest& req) {
// update m_settingsConn
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_settingsConn = std::move(connPtr);
}
@@ -338,7 +338,7 @@ void HttpCameraImpl::DeviceSendSettings(wpi::HttpRequest& req) {
}
CS_HttpCameraKind HttpCameraImpl::GetKind() const {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
return m_kind;
}
@@ -356,7 +356,7 @@ bool HttpCameraImpl::SetUrls(wpi::ArrayRef<std::string> urls,
}
}
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_locations.swap(locations);
m_nextLocation = 0;
m_streamSettingsUpdated = true;
@@ -364,7 +364,7 @@ bool HttpCameraImpl::SetUrls(wpi::ArrayRef<std::string> urls,
}
std::vector<std::string> HttpCameraImpl::GetUrls() const {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
std::vector<std::string> urls;
for (const auto& loc : m_locations) urls.push_back(loc.url);
return urls;
@@ -375,7 +375,7 @@ void HttpCameraImpl::CreateProperty(const wpi::Twine& name,
bool viaSettings, CS_PropertyKind kind,
int minimum, int maximum, int step,
int defaultValue, int value) const {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_propertyData.emplace_back(std::make_unique<PropertyData>(
name, httpParam, viaSettings, kind, minimum, maximum, step, defaultValue,
value));
@@ -389,7 +389,7 @@ template <typename T>
void HttpCameraImpl::CreateEnumProperty(
const wpi::Twine& name, const wpi::Twine& httpParam, bool viaSettings,
int defaultValue, int value, std::initializer_list<T> choices) const {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_propertyData.emplace_back(std::make_unique<PropertyData>(
name, httpParam, viaSettings, CS_PROP_ENUM, 0, choices.size() - 1, 1,
defaultValue, value));
@@ -412,7 +412,7 @@ std::unique_ptr<PropertyImpl> HttpCameraImpl::CreateEmptyProperty(
}
bool HttpCameraImpl::CacheProperties(CS_Status* status) const {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
// Pretty typical set of video modes
m_videoModes.clear();
@@ -468,7 +468,7 @@ void HttpCameraImpl::SetExposureManual(int value, CS_Status* status) {
bool HttpCameraImpl::SetVideoMode(const VideoMode& mode, CS_Status* status) {
if (mode.pixelFormat != VideoMode::kMJPEG) return false;
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_mode = mode;
m_streamSettingsUpdated = true;
return true;
@@ -497,7 +497,7 @@ bool AxisCameraImpl::CacheProperties(CS_Status* status) const {
true, CS_PROP_INTEGER, 0, 100, 1, 50, 50);
// TODO: get video modes from device
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_videoModes.clear();
m_videoModes.emplace_back(VideoMode::kMJPEG, 640, 480, 30);
m_videoModes.emplace_back(VideoMode::kMJPEG, 480, 360, 30);