mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Make SafeThread header-only. (#117)
This commit is contained in:
@@ -76,6 +76,27 @@ class SafeThreadOwnerBase {
|
||||
std::atomic<SafeThread*> m_thread;
|
||||
};
|
||||
|
||||
inline void SafeThreadOwnerBase::Start(SafeThread* thr) {
|
||||
SafeThread* curthr = nullptr;
|
||||
SafeThread* newthr = thr;
|
||||
if (!m_thread.compare_exchange_strong(curthr, newthr)) {
|
||||
delete newthr;
|
||||
return;
|
||||
}
|
||||
std::thread([=]() {
|
||||
newthr->Main();
|
||||
delete newthr;
|
||||
}).detach();
|
||||
}
|
||||
|
||||
inline void SafeThreadOwnerBase::Stop() {
|
||||
SafeThread* thr = m_thread.exchange(nullptr);
|
||||
if (!thr) return;
|
||||
std::lock_guard<std::mutex> lock(thr->m_mutex);
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_one();
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename T>
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/* Copyright (c) FIRST 2015. 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 "support/SafeThread.h"
|
||||
|
||||
using namespace wpi;
|
||||
|
||||
void detail::SafeThreadOwnerBase::Start(SafeThread* thr) {
|
||||
SafeThread* curthr = nullptr;
|
||||
SafeThread* newthr = thr;
|
||||
if (!m_thread.compare_exchange_strong(curthr, newthr)) {
|
||||
delete newthr;
|
||||
return;
|
||||
}
|
||||
std::thread([=]() {
|
||||
newthr->Main();
|
||||
delete newthr;
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void detail::SafeThreadOwnerBase::Stop() {
|
||||
SafeThread* thr = m_thread.exchange(nullptr);
|
||||
if (!thr) return;
|
||||
std::lock_guard<std::mutex> lock(thr->m_mutex);
|
||||
thr->m_active = false;
|
||||
thr->m_cond.notify_one();
|
||||
}
|
||||
Reference in New Issue
Block a user