2015-12-17 16:19:44 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2015-2016. All Rights Reserved. */
|
2015-12-17 16:19:44 -08:00
|
|
|
/* 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 "SafeThread.h"
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
// using namespace nt;
|
2015-12-17 16:19:44 -08:00
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|