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

@@ -87,7 +87,7 @@ static std::string MakeStreamValue(const wpi::Twine& address, int port) {
std::shared_ptr<nt::NetworkTable> CameraServer::Impl::GetSourceTable(
CS_Source source) {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
return m_tables.lookup(source);
}
@@ -153,7 +153,7 @@ std::vector<std::string> CameraServer::Impl::GetSourceStreamValues(
}
void CameraServer::Impl::UpdateStreamValues() {
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
// Over all the sinks...
for (const auto& i : m_sinks) {
CS_Status status = 0;
@@ -299,7 +299,7 @@ CameraServer::Impl::Impl()
// Create subtable for the camera
auto table = m_publishTable->GetSubTable(event.name);
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_tables.insert(std::make_pair(event.sourceHandle, table));
}
wpi::SmallString<64> buf;
@@ -564,7 +564,7 @@ cs::CvSink CameraServer::GetVideo() {
cs::VideoSource source;
{
auto csShared = GetCameraServerShared();
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
if (m_impl->m_primarySourceName.empty()) {
csShared->SetCameraServerError("no camera available");
return cs::CvSink{};
@@ -584,7 +584,7 @@ cs::CvSink CameraServer::GetVideo(const cs::VideoSource& camera) {
name += camera.GetName();
{
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
auto it = m_impl->m_sinks.find(name);
if (it != m_impl->m_sinks.end()) {
auto kind = it->second.GetKind();
@@ -609,7 +609,7 @@ cs::CvSink CameraServer::GetVideo(const wpi::Twine& name) {
wpi::StringRef nameStr = name.toStringRef(nameBuf);
cs::VideoSource source;
{
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
auto it = m_impl->m_sources.find(nameStr);
if (it == m_impl->m_sources.end()) {
auto csShared = GetCameraServerShared();
@@ -631,7 +631,7 @@ cs::CvSource CameraServer::PutVideo(const wpi::Twine& name, int width,
cs::MjpegServer CameraServer::AddServer(const wpi::Twine& name) {
int port;
{
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
port = m_impl->m_nextPort++;
}
return AddServer(name, port);
@@ -644,12 +644,12 @@ cs::MjpegServer CameraServer::AddServer(const wpi::Twine& name, int port) {
}
void CameraServer::AddServer(const cs::VideoSink& server) {
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
m_impl->m_sinks.try_emplace(server.GetName(), server);
}
void CameraServer::RemoveServer(const wpi::Twine& name) {
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
wpi::SmallString<64> nameBuf;
m_impl->m_sinks.erase(name.toStringRef(nameBuf));
}
@@ -657,7 +657,7 @@ void CameraServer::RemoveServer(const wpi::Twine& name) {
cs::VideoSink CameraServer::GetServer() {
wpi::SmallString<64> name;
{
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
if (m_impl->m_primarySourceName.empty()) {
auto csShared = GetCameraServerShared();
csShared->SetCameraServerError("no camera available");
@@ -672,7 +672,7 @@ cs::VideoSink CameraServer::GetServer() {
cs::VideoSink CameraServer::GetServer(const wpi::Twine& name) {
wpi::SmallString<64> nameBuf;
wpi::StringRef nameStr = name.toStringRef(nameBuf);
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
auto it = m_impl->m_sinks.find(nameStr);
if (it == m_impl->m_sinks.end()) {
auto csShared = GetCameraServerShared();
@@ -684,19 +684,19 @@ cs::VideoSink CameraServer::GetServer(const wpi::Twine& name) {
void CameraServer::AddCamera(const cs::VideoSource& camera) {
std::string name = camera.GetName();
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
if (m_impl->m_primarySourceName.empty()) m_impl->m_primarySourceName = name;
m_impl->m_sources.try_emplace(name, camera);
}
void CameraServer::RemoveCamera(const wpi::Twine& name) {
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
wpi::SmallString<64> nameBuf;
m_impl->m_sources.erase(name.toStringRef(nameBuf));
}
void CameraServer::SetSize(int size) {
std::lock_guard lock(m_impl->m_mutex);
std::scoped_lock lock(m_impl->m_mutex);
if (m_impl->m_primarySourceName.empty()) return;
auto it = m_impl->m_sources.find(m_impl->m_primarySourceName);
if (it == m_impl->m_sources.end()) return;