[wpiutil] SafeThread: Add Synchronization object variant (#4382)

This enables use of SafeThread with Synchronization's WaitForObjects() blocking.
This commit is contained in:
Peter Johnson
2022-08-31 11:29:57 -07:00
committed by GitHub
parent 4254438d8d
commit cc56bdc787
2 changed files with 42 additions and 17 deletions

View File

@@ -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();
}