mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
Remove template types from lock RAII wrapper usages (#1756)
C++17 has template type autodeduction. These wrappers include std::lock_guard and std::unique_lock.
This commit is contained in:
committed by
Peter Johnson
parent
e582518bae
commit
841ef5d739
@@ -29,7 +29,7 @@ detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() {
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
std::lock_guard 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<wpi::mutex> lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) {
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_all();
|
||||
@@ -47,7 +47,7 @@ void detail::SafeThreadOwnerBase::Stop() {
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Join() {
|
||||
std::unique_lock<wpi::mutex> lock(m_mutex);
|
||||
std::unique_lock lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) {
|
||||
auto stdThread = std::move(m_stdThread);
|
||||
m_thread.reset();
|
||||
@@ -64,25 +64,25 @@ 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<wpi::mutex> lock_lhs(lhs.m_mutex, std::adopt_lock);
|
||||
std::lock_guard<wpi::mutex> lock_rhs(rhs.m_mutex, std::adopt_lock);
|
||||
std::lock_guard lock_lhs(lhs.m_mutex, std::adopt_lock);
|
||||
std::lock_guard lock_rhs(rhs.m_mutex, std::adopt_lock);
|
||||
std::swap(lhs.m_stdThread, rhs.m_stdThread);
|
||||
std::swap(lhs.m_thread, rhs.m_thread);
|
||||
}
|
||||
|
||||
detail::SafeThreadOwnerBase::operator bool() const {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
return !m_thread.expired();
|
||||
}
|
||||
|
||||
std::thread::native_handle_type
|
||||
detail::SafeThreadOwnerBase::GetNativeThreadHandle() {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
return m_stdThread.native_handle();
|
||||
}
|
||||
|
||||
std::shared_ptr<SafeThread> detail::SafeThreadOwnerBase::GetThreadSharedPtr()
|
||||
const {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
std::lock_guard lock(m_mutex);
|
||||
return m_thread.lock();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2017-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. */
|
||||
@@ -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<wpi::mutex> lock(local->mtx);
|
||||
std::lock_guard 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<wpi::mutex> lock(local->mtx);
|
||||
std::lock_guard 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<wpi::mutex> lock(local->mtx);
|
||||
std::lock_guard lock(local->mtx);
|
||||
local->active.erase(active_tracker);
|
||||
}
|
||||
|
||||
// successful connection
|
||||
if (stream) {
|
||||
std::lock_guard<wpi::mutex> lock(result->mtx);
|
||||
std::lock_guard lock(result->mtx);
|
||||
if (!result->done.exchange(true)) result->stream = std::move(stream);
|
||||
}
|
||||
}
|
||||
@@ -112,7 +112,7 @@ std::unique_ptr<NetworkStream> TCPConnector::connect_parallel(
|
||||
}
|
||||
|
||||
// wait for a result, timeout, or all finished
|
||||
std::unique_lock<wpi::mutex> lock(result->mtx);
|
||||
std::unique_lock lock(result->mtx);
|
||||
if (timeout == 0) {
|
||||
result->cv.wait(
|
||||
lock, [&] { return result->stream || result->count >= num_workers; });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
||||
/* Copyright (c) 2018-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. */
|
||||
@@ -16,12 +16,12 @@ PromiseFactoryBase::~PromiseFactoryBase() {
|
||||
}
|
||||
|
||||
void PromiseFactoryBase::IgnoreResult(uint64_t request) {
|
||||
std::unique_lock<wpi::mutex> lock(m_resultMutex);
|
||||
std::unique_lock lock(m_resultMutex);
|
||||
EraseRequest(request);
|
||||
}
|
||||
|
||||
uint64_t PromiseFactoryBase::CreateRequest() {
|
||||
std::unique_lock<wpi::mutex> lock(m_resultMutex);
|
||||
std::unique_lock lock(m_resultMutex);
|
||||
uint64_t req = ++m_uid;
|
||||
m_requests.push_back(req);
|
||||
return req;
|
||||
@@ -39,14 +39,14 @@ bool PromiseFactoryBase::EraseRequest(uint64_t request) {
|
||||
} // namespace detail
|
||||
|
||||
future<void> PromiseFactory<void>::MakeReadyFuture() {
|
||||
std::unique_lock<wpi::mutex> lock(GetResultMutex());
|
||||
std::unique_lock lock(GetResultMutex());
|
||||
uint64_t req = CreateErasedRequest();
|
||||
m_results.emplace_back(req);
|
||||
return future<void>{this, req};
|
||||
}
|
||||
|
||||
void PromiseFactory<void>::SetValue(uint64_t request) {
|
||||
std::unique_lock<wpi::mutex> lock(GetResultMutex());
|
||||
std::unique_lock lock(GetResultMutex());
|
||||
if (!EraseRequest(request)) return;
|
||||
auto it = std::find_if(m_thens.begin(), m_thens.end(),
|
||||
[=](const auto& x) { return x.request == request; });
|
||||
@@ -63,7 +63,7 @@ void PromiseFactory<void>::SetValue(uint64_t request) {
|
||||
|
||||
void PromiseFactory<void>::SetThen(uint64_t request, uint64_t outRequest,
|
||||
ThenFunction func) {
|
||||
std::unique_lock<wpi::mutex> lock(GetResultMutex());
|
||||
std::unique_lock lock(GetResultMutex());
|
||||
auto it = std::find_if(m_results.begin(), m_results.end(),
|
||||
[=](const auto& r) { return r == request; });
|
||||
if (it != m_results.end()) {
|
||||
@@ -75,7 +75,7 @@ void PromiseFactory<void>::SetThen(uint64_t request, uint64_t outRequest,
|
||||
}
|
||||
|
||||
bool PromiseFactory<void>::IsReady(uint64_t request) noexcept {
|
||||
std::unique_lock<wpi::mutex> lock(GetResultMutex());
|
||||
std::unique_lock lock(GetResultMutex());
|
||||
auto it = std::find_if(m_results.begin(), m_results.end(),
|
||||
[=](const auto& r) { return r == request; });
|
||||
return it != m_results.end();
|
||||
@@ -83,7 +83,7 @@ bool PromiseFactory<void>::IsReady(uint64_t request) noexcept {
|
||||
|
||||
void PromiseFactory<void>::GetResult(uint64_t request) {
|
||||
// wait for response
|
||||
std::unique_lock<wpi::mutex> lock(GetResultMutex());
|
||||
std::unique_lock lock(GetResultMutex());
|
||||
while (IsActive()) {
|
||||
// Did we get a response to *our* request?
|
||||
auto it = std::find_if(m_results.begin(), m_results.end(),
|
||||
@@ -100,7 +100,7 @@ void PromiseFactory<void>::GetResult(uint64_t request) {
|
||||
|
||||
void PromiseFactory<void>::WaitResult(uint64_t request) {
|
||||
// wait for response
|
||||
std::unique_lock<wpi::mutex> lock(GetResultMutex());
|
||||
std::unique_lock lock(GetResultMutex());
|
||||
while (IsActive()) {
|
||||
// Did we get a response to *our* request?
|
||||
auto it = std::find_if(m_results.begin(), m_results.end(),
|
||||
|
||||
@@ -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<std::mutex> Lock(ErrorHandlerMutex);
|
||||
std::lock_guard Lock(ErrorHandlerMutex);
|
||||
assert(!ErrorHandler && "Error handler already registered!\n");
|
||||
ErrorHandler = handler;
|
||||
ErrorHandlerUserData = user_data;
|
||||
}
|
||||
|
||||
void wpi::remove_fatal_error_handler() {
|
||||
std::lock_guard<std::mutex> Lock(ErrorHandlerMutex);
|
||||
std::lock_guard 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<std::mutex> Lock(ErrorHandlerMutex);
|
||||
std::lock_guard 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<std::mutex> Lock(BadAllocErrorHandlerMutex);
|
||||
std::lock_guard 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<std::mutex> Lock(BadAllocErrorHandlerMutex);
|
||||
std::lock_guard 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<std::mutex> Lock(BadAllocErrorHandlerMutex);
|
||||
std::lock_guard 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<wpi::mutex> Lock(*getManagedStaticMutex());
|
||||
std::lock_guard 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<wpi::mutex> Lock(*getManagedStaticMutex());
|
||||
std::lock_guard Lock(*getManagedStaticMutex());
|
||||
|
||||
while (StaticList)
|
||||
StaticList->destroy();
|
||||
|
||||
Reference in New Issue
Block a user