8 #ifndef WPIUTIL_WPI_SAFETHREAD_H_
9 #define WPIUTIL_WPI_SAFETHREAD_H_
14 #include "wpi/condition_variable.h"
15 #include "wpi/mutex.h"
24 virtual void Main() = 0;
27 std::atomic_bool m_active;
28 wpi::condition_variable m_cond;
37 if (!m_thread)
return;
38 m_lock = std::unique_lock<wpi::mutex>(m_thread->m_mutex);
39 if (!m_thread->m_active) {
45 explicit operator bool()
const {
return m_thread !=
nullptr; }
46 std::unique_lock<wpi::mutex>& GetLock() {
return m_lock; }
50 std::unique_lock<wpi::mutex> m_lock;
59 T& operator*()
const {
return *
static_cast<T*
>(m_thread); }
60 T* operator->()
const {
return static_cast<T*
>(m_thread); }
72 : m_thread(other.m_thread.exchange(
nullptr)) {}
74 SafeThread* otherthr = other.m_thread.exchange(
nullptr);
75 SafeThread* curthr = m_thread.exchange(otherthr);
76 other.m_thread.exchange(curthr);
81 explicit operator bool()
const {
return m_thread.load(); }
85 SafeThread* GetThread()
const {
return m_thread.load(); }
86 std::thread::native_handle_type GetNativeThreadHandle()
const {
87 return m_nativeHandle;
91 std::atomic<SafeThread*> m_thread;
92 std::atomic<std::thread::native_handle_type> m_nativeHandle;
95 inline void SafeThreadOwnerBase::Start(
SafeThread* thr) {
98 if (!m_thread.compare_exchange_strong(curthr, newthr)) {
102 std::thread stdThread([=]() {
106 m_nativeHandle = stdThread.native_handle();
110 inline void SafeThreadOwnerBase::Stop() {
111 SafeThread* thr = m_thread.exchange(
nullptr);
113 thr->m_active =
false;
114 thr->m_cond.notify_one();
119 template <
typename T>
122 void Start() { Start(
new T); }
123 void Start(T* thr) { detail::SafeThreadOwnerBase::Start(thr); }
126 Proxy GetThread()
const {
127 return Proxy(detail::SafeThreadOwnerBase::GetThread());
133 #endif // WPIUTIL_WPI_SAFETHREAD_H_
Definition: SafeThread.h:56
Definition: SafeThread.h:20
Definition: SafeThread.h:120
namespace to hold default to_json function
Definition: SmallString.h:21
Definition: SafeThread.h:64
Definition: SafeThread.h:34