mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[wpiutil] SafeThread: Add Synchronization object variant (#4382)
This enables use of SafeThread with Synchronization's WaitForObjects() blocking.
This commit is contained in:
@@ -6,8 +6,18 @@
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
void SafeThread::Stop() {
|
||||
m_active = false;
|
||||
m_cond.notify_all();
|
||||
}
|
||||
|
||||
void SafeThreadEvent::Stop() {
|
||||
m_active = false;
|
||||
m_stopEvent.Set();
|
||||
}
|
||||
|
||||
detail::SafeThreadProxyBase::SafeThreadProxyBase(
|
||||
std::shared_ptr<SafeThread> thr)
|
||||
std::shared_ptr<SafeThreadBase> thr)
|
||||
: m_thread(std::move(thr)) {
|
||||
if (!m_thread) {
|
||||
return;
|
||||
@@ -28,7 +38,7 @@ detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() {
|
||||
}
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
|
||||
void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThreadBase> thr) {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) {
|
||||
return;
|
||||
@@ -41,8 +51,7 @@ void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
|
||||
void detail::SafeThreadOwnerBase::Stop() {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
if (auto thr = m_thread.lock()) {
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_all();
|
||||
thr->Stop();
|
||||
m_thread.reset();
|
||||
}
|
||||
if (m_stdThread.joinable()) {
|
||||
@@ -56,8 +65,7 @@ void detail::SafeThreadOwnerBase::Join() {
|
||||
auto stdThread = std::move(m_stdThread);
|
||||
m_thread.reset();
|
||||
lock.unlock();
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_all();
|
||||
thr->Stop();
|
||||
stdThread.join();
|
||||
} else if (m_stdThread.joinable()) {
|
||||
m_stdThread.detach();
|
||||
@@ -85,8 +93,8 @@ detail::SafeThreadOwnerBase::GetNativeThreadHandle() {
|
||||
return m_stdThread.native_handle();
|
||||
}
|
||||
|
||||
std::shared_ptr<SafeThread> detail::SafeThreadOwnerBase::GetThreadSharedPtr()
|
||||
const {
|
||||
std::shared_ptr<SafeThreadBase>
|
||||
detail::SafeThreadOwnerBase::GetThreadSharedPtr() const {
|
||||
std::scoped_lock lock(m_mutex);
|
||||
return m_thread.lock();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user