mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
SafeThread: Avoid use-after-free risk in thread shutdown (#1355)
Use shared_ptr to keep data alive until the thread has terminated.
This commit is contained in:
65
wpiutil/src/main/native/cpp/SafeThread.cpp
Normal file
65
wpiutil/src/main/native/cpp/SafeThread.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) 2015-2018 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. */
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
#include "wpi/SafeThread.h"
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
detail::SafeThreadProxyBase::SafeThreadProxyBase(
|
||||
std::shared_ptr<SafeThread> thr)
|
||||
: m_thread(std::move(thr)) {
|
||||
if (!m_thread) return;
|
||||
m_lock = std::unique_lock<wpi::mutex>(m_thread->m_mutex);
|
||||
if (!m_thread->m_active) {
|
||||
m_lock.unlock();
|
||||
m_thread = nullptr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) return;
|
||||
std::thread stdThread([=] { thr->Main(); });
|
||||
m_thread = thr;
|
||||
m_nativeHandle = stdThread.native_handle();
|
||||
stdThread.detach();
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Stop() {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) {
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
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::swap(lhs.m_thread, rhs.m_thread);
|
||||
std::swap(lhs.m_nativeHandle, rhs.m_nativeHandle);
|
||||
}
|
||||
|
||||
detail::SafeThreadOwnerBase::operator bool() const {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
return !m_thread.expired();
|
||||
}
|
||||
|
||||
std::thread::native_handle_type
|
||||
detail::SafeThreadOwnerBase::GetNativeThreadHandle() const {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
return m_nativeHandle;
|
||||
}
|
||||
|
||||
std::shared_ptr<SafeThread> detail::SafeThreadOwnerBase::GetThread() const {
|
||||
std::lock_guard<wpi::mutex> lock(m_mutex);
|
||||
return m_thread.lock();
|
||||
}
|
||||
Reference in New Issue
Block a user