WPILibC++  2018.4.1-20180923204725-1195-g5c6b78e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
SafeThread.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_SAFETHREAD_H_
9 #define WPIUTIL_WPI_SAFETHREAD_H_
10 
11 #include <atomic>
12 #include <thread>
13 
14 #include "wpi/condition_variable.h"
15 #include "wpi/mutex.h"
16 
17 namespace wpi {
18 
19 // Base class for SafeThreadOwner threads.
20 class SafeThread {
21  public:
22  SafeThread() { m_active = true; }
23  virtual ~SafeThread() = default;
24  virtual void Main() = 0;
25 
26  wpi::mutex m_mutex;
27  std::atomic_bool m_active;
28  wpi::condition_variable m_cond;
29 };
30 
31 namespace detail {
32 
33 // Non-template proxy base class for common proxy code.
35  public:
36  explicit SafeThreadProxyBase(SafeThread* thr) : m_thread(thr) {
37  if (!m_thread) return;
38  m_lock = std::unique_lock<wpi::mutex>(m_thread->m_mutex);
39  if (!m_thread->m_active) {
40  m_lock.unlock();
41  m_thread = nullptr;
42  return;
43  }
44  }
45  explicit operator bool() const { return m_thread != nullptr; }
46  std::unique_lock<wpi::mutex>& GetLock() { return m_lock; }
47 
48  protected:
49  SafeThread* m_thread;
50  std::unique_lock<wpi::mutex> m_lock;
51 };
52 
53 // A proxy for SafeThread.
54 // Also serves as a scoped lock on SafeThread::m_mutex.
55 template <typename T>
57  public:
58  explicit SafeThreadProxy(SafeThread* thr) : SafeThreadProxyBase(thr) {}
59  T& operator*() const { return *static_cast<T*>(m_thread); }
60  T* operator->() const { return static_cast<T*>(m_thread); }
61 };
62 
63 // Non-template owner base class for common owner code.
65  public:
66  void Stop();
67 
68  SafeThreadOwnerBase() { m_thread = nullptr; }
70  SafeThreadOwnerBase& operator=(const SafeThreadOwnerBase&) = delete;
72  : m_thread(other.m_thread.exchange(nullptr)) {}
73  SafeThreadOwnerBase& operator=(SafeThreadOwnerBase other) {
74  SafeThread* otherthr = other.m_thread.exchange(nullptr);
75  SafeThread* curthr = m_thread.exchange(otherthr);
76  other.m_thread.exchange(curthr); // other destructor will clean up
77  return *this;
78  }
79  ~SafeThreadOwnerBase() { Stop(); }
80 
81  explicit operator bool() const { return m_thread.load(); }
82 
83  protected:
84  void Start(SafeThread* thr);
85  SafeThread* GetThread() const { return m_thread.load(); }
86  std::thread::native_handle_type GetNativeThreadHandle() const {
87  return m_nativeHandle;
88  }
89 
90  private:
91  std::atomic<SafeThread*> m_thread;
92  std::atomic<std::thread::native_handle_type> m_nativeHandle;
93 };
94 
95 inline void SafeThreadOwnerBase::Start(SafeThread* thr) {
96  SafeThread* curthr = nullptr;
97  SafeThread* newthr = thr;
98  if (!m_thread.compare_exchange_strong(curthr, newthr)) {
99  delete newthr;
100  return;
101  }
102  std::thread stdThread([=]() {
103  newthr->Main();
104  delete newthr;
105  });
106  m_nativeHandle = stdThread.native_handle();
107  stdThread.detach();
108 }
109 
110 inline void SafeThreadOwnerBase::Stop() {
111  SafeThread* thr = m_thread.exchange(nullptr);
112  if (!thr) return;
113  thr->m_active = false;
114  thr->m_cond.notify_one();
115 }
116 
117 } // namespace detail
118 
119 template <typename T>
121  public:
122  void Start() { Start(new T); }
123  void Start(T* thr) { detail::SafeThreadOwnerBase::Start(thr); }
124 
125  using Proxy = typename detail::SafeThreadProxy<T>;
126  Proxy GetThread() const {
127  return Proxy(detail::SafeThreadOwnerBase::GetThread());
128  }
129 };
130 
131 } // namespace wpi
132 
133 #endif // WPIUTIL_WPI_SAFETHREAD_H_
Definition: SafeThread.h:56
Definition: SafeThread.h:20
Definition: SafeThread.h:120
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: SafeThread.h:64
Definition: SafeThread.h:34