mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
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:
committed by
Peter Johnson
parent
24d31df55a
commit
62be0392b6
@@ -29,7 +29,7 @@ detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() {
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) return;
|
||||
m_stdThread = std::thread([=] { thr->Main(); });
|
||||
thr->m_threadId = m_stdThread.get_id();
|
||||
@@ -37,7 +37,7 @@ void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Stop() {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) {
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_all();
|
||||
@@ -63,26 +63,24 @@ void detail::SafeThreadOwnerBase::Join() {
|
||||
void detail::swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept {
|
||||
using std::swap;
|
||||
if (&lhs == &rhs) return;
|
||||
std::lock(lhs.m_mutex, rhs.m_mutex);
|
||||
std::lock_guard lock_lhs(lhs.m_mutex, std::adopt_lock);
|
||||
std::lock_guard lock_rhs(rhs.m_mutex, std::adopt_lock);
|
||||
std::scoped_lock lock(lhs.m_mutex, rhs.m_mutex);
|
||||
std::swap(lhs.m_stdThread, rhs.m_stdThread);
|
||||
std::swap(lhs.m_thread, rhs.m_thread);
|
||||
}
|
||||
|
||||
detail::SafeThreadOwnerBase::operator bool() const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
return !m_thread.expired();
|
||||
}
|
||||
|
||||
std::thread::native_handle_type
|
||||
detail::SafeThreadOwnerBase::GetNativeThreadHandle() {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
return m_stdThread.native_handle();
|
||||
}
|
||||
|
||||
std::shared_ptr<SafeThread> detail::SafeThreadOwnerBase::GetThreadSharedPtr()
|
||||
const {
|
||||
std::lock_guard lock(m_mutex);
|
||||
std::scoped_lock lock(m_mutex);
|
||||
return m_thread.lock();
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect_parallel(
|
||||
// don't start a new worker if we had a previously still-active connection
|
||||
// attempt to the same server
|
||||
{
|
||||
std::lock_guard lock(local->mtx);
|
||||
std::scoped_lock lock(local->mtx);
|
||||
if (local->active.count(active_tracker) > 0) continue; // already in set
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect_parallel(
|
||||
if (!result->done) {
|
||||
// add to global state
|
||||
{
|
||||
std::lock_guard lock(local->mtx);
|
||||
std::scoped_lock lock(local->mtx);
|
||||
local->active.insert(active_tracker);
|
||||
}
|
||||
|
||||
@@ -95,13 +95,13 @@ std::unique_ptr<NetworkStream> TCPConnector::connect_parallel(
|
||||
|
||||
// remove from global state
|
||||
{
|
||||
std::lock_guard lock(local->mtx);
|
||||
std::scoped_lock lock(local->mtx);
|
||||
local->active.erase(active_tracker);
|
||||
}
|
||||
|
||||
// successful connection
|
||||
if (stream) {
|
||||
std::lock_guard lock(result->mtx);
|
||||
std::scoped_lock lock(result->mtx);
|
||||
if (!result->done.exchange(true)) result->stream = std::move(stream);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,14 +55,14 @@ static std::mutex BadAllocErrorHandlerMutex;
|
||||
|
||||
void wpi::install_fatal_error_handler(fatal_error_handler_t handler,
|
||||
void *user_data) {
|
||||
std::lock_guard Lock(ErrorHandlerMutex);
|
||||
std::scoped_lock Lock(ErrorHandlerMutex);
|
||||
assert(!ErrorHandler && "Error handler already registered!\n");
|
||||
ErrorHandler = handler;
|
||||
ErrorHandlerUserData = user_data;
|
||||
}
|
||||
|
||||
void wpi::remove_fatal_error_handler() {
|
||||
std::lock_guard Lock(ErrorHandlerMutex);
|
||||
std::scoped_lock Lock(ErrorHandlerMutex);
|
||||
ErrorHandler = nullptr;
|
||||
ErrorHandlerUserData = nullptr;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ void wpi::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
|
||||
{
|
||||
// Only acquire the mutex while reading the handler, so as not to invoke a
|
||||
// user-supplied callback under a lock.
|
||||
std::lock_guard Lock(ErrorHandlerMutex);
|
||||
std::scoped_lock Lock(ErrorHandlerMutex);
|
||||
handler = ErrorHandler;
|
||||
handlerData = ErrorHandlerUserData;
|
||||
}
|
||||
@@ -113,14 +113,14 @@ void wpi::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
|
||||
|
||||
void wpi::install_bad_alloc_error_handler(fatal_error_handler_t handler,
|
||||
void *user_data) {
|
||||
std::lock_guard Lock(BadAllocErrorHandlerMutex);
|
||||
std::scoped_lock Lock(BadAllocErrorHandlerMutex);
|
||||
assert(!ErrorHandler && "Bad alloc error handler already registered!\n");
|
||||
BadAllocErrorHandler = handler;
|
||||
BadAllocErrorHandlerUserData = user_data;
|
||||
}
|
||||
|
||||
void wpi::remove_bad_alloc_error_handler() {
|
||||
std::lock_guard Lock(BadAllocErrorHandlerMutex);
|
||||
std::scoped_lock Lock(BadAllocErrorHandlerMutex);
|
||||
BadAllocErrorHandler = nullptr;
|
||||
BadAllocErrorHandlerUserData = nullptr;
|
||||
}
|
||||
@@ -131,7 +131,7 @@ void wpi::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) {
|
||||
{
|
||||
// Only acquire the mutex while reading the handler, so as not to invoke a
|
||||
// user-supplied callback under a lock.
|
||||
std::lock_guard Lock(BadAllocErrorHandlerMutex);
|
||||
std::scoped_lock Lock(BadAllocErrorHandlerMutex);
|
||||
Handler = BadAllocErrorHandler;
|
||||
HandlerData = BadAllocErrorHandlerUserData;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ static wpi::mutex* getManagedStaticMutex() {
|
||||
void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(),
|
||||
void (*Deleter)(void*)) const {
|
||||
assert(Creator);
|
||||
std::lock_guard Lock(*getManagedStaticMutex());
|
||||
std::scoped_lock Lock(*getManagedStaticMutex());
|
||||
|
||||
if (!Ptr.load(std::memory_order_relaxed)) {
|
||||
void *Tmp = Creator();
|
||||
@@ -65,7 +65,7 @@ void ManagedStaticBase::destroy() const {
|
||||
|
||||
/// wpi_shutdown - Deallocate and destroy all ManagedStatic variables.
|
||||
void wpi::wpi_shutdown() {
|
||||
std::lock_guard Lock(*getManagedStaticMutex());
|
||||
std::scoped_lock Lock(*getManagedStaticMutex());
|
||||
|
||||
while (StaticList)
|
||||
StaticList->destroy();
|
||||
|
||||
Reference in New Issue
Block a user