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

@@ -548,9 +548,7 @@ public:
}
SignalBase & operator=(SignalBase && o) {
lock_type lock1(m_mutex, std::defer_lock);
lock_type lock2(o.m_mutex, std::defer_lock);
std::lock(lock1, lock2);
std::scoped_lock lock(m_mutex, o.m_mutex);
std::swap(m_func, o.m_func);
m_block.store(o.m_block.exchange(m_block.load()));

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2016-2019 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. */
@@ -7,7 +7,7 @@
#pragma once
// Allows usage with std::lock_guard without including <mutex> separately
// Allows usage with std::scoped_lock without including <mutex> separately
#ifdef __linux__
#include <pthread.h>
#endif

View File

@@ -68,7 +68,7 @@ class Async final : public HandleImpl<Async<T...>, uv_async_t> {
int err =
uv_async_init(loop->GetRaw(), h->GetRaw(), [](uv_async_t* handle) {
auto& h = *static_cast<Async*>(handle->data);
std::lock_guard lock(h.m_mutex);
std::scoped_lock lock(h.m_mutex);
for (auto&& v : h.m_data) std::apply(h.wakeup, v);
h.m_data.clear();
});
@@ -96,7 +96,7 @@ class Async final : public HandleImpl<Async<T...>, uv_async_t> {
}
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_data.emplace_back(std::forward_as_tuple(std::forward<U>(u)...));
}
if (loop) this->Invoke(&uv_async_send, this->GetRaw());

View File

@@ -132,7 +132,7 @@ class AsyncFunction<R(T...)> final
// add the parameters to the input queue
{
std::lock_guard lock(m_mutex);
std::scoped_lock lock(m_mutex);
m_params.emplace_back(std::piecewise_construct,
std::forward_as_tuple(req),
std::forward_as_tuple(std::forward<U>(u)...));