From 760d6a26d3b05d2c359cf2c0d9e5cb0d2e3c863e Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sun, 25 Sep 2016 18:21:29 -0700 Subject: [PATCH] Make SafeThread header-only. (#117) --- include/support/SafeThread.h | 21 +++++++++++++++++++++ src/support/SafeThread.cpp | 31 ------------------------------- 2 files changed, 21 insertions(+), 31 deletions(-) delete mode 100644 src/support/SafeThread.cpp diff --git a/include/support/SafeThread.h b/include/support/SafeThread.h index 2edbe7ae3c..2cb9ba9031 100644 --- a/include/support/SafeThread.h +++ b/include/support/SafeThread.h @@ -76,6 +76,27 @@ class SafeThreadOwnerBase { std::atomic 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 lock(thr->m_mutex); + thr->m_active = false; + thr->m_cond.notify_one(); +} + } // namespace detail template diff --git a/src/support/SafeThread.cpp b/src/support/SafeThread.cpp deleted file mode 100644 index 434d933b8b..0000000000 --- a/src/support/SafeThread.cpp +++ /dev/null @@ -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 lock(thr->m_mutex); - thr->m_active = false; - thr->m_cond.notify_one(); -}