WPILibC++  2020.3.2
SafeThread.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2019 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 <memory>
13 #include <thread>
14 #include <utility>
15 
16 #include "wpi/condition_variable.h"
17 #include "wpi/mutex.h"
18 
19 namespace wpi {
20 
21 // Base class for SafeThreadOwner threads.
22 class SafeThread {
23  public:
24  virtual ~SafeThread() = default;
25  virtual void Main() = 0;
26 
27  mutable wpi::mutex m_mutex;
28  std::atomic_bool m_active{true};
29  wpi::condition_variable m_cond;
30  std::thread::id m_threadId;
31 };
32 
33 namespace detail {
34 
35 // Non-template proxy base class for common proxy code.
37  public:
38  explicit SafeThreadProxyBase(std::shared_ptr<SafeThread> thr);
39  explicit operator bool() const { return m_thread != nullptr; }
40  std::unique_lock<wpi::mutex>& GetLock() { return m_lock; }
41 
42  protected:
43  std::shared_ptr<SafeThread> m_thread;
44  std::unique_lock<wpi::mutex> m_lock;
45 };
46 
47 // A proxy for SafeThread.
48 // Also serves as a scoped lock on SafeThread::m_mutex.
49 template <typename T>
51  public:
52  explicit SafeThreadProxy(std::shared_ptr<SafeThread> thr)
53  : SafeThreadProxyBase(std::move(thr)) {}
54  T& operator*() const { return *static_cast<T*>(m_thread.get()); }
55  T* operator->() const { return static_cast<T*>(m_thread.get()); }
56 };
57 
58 // Non-template owner base class for common owner code.
60  public:
61  void Stop();
62  void Join();
63 
64  SafeThreadOwnerBase() noexcept = default;
66  SafeThreadOwnerBase& operator=(const SafeThreadOwnerBase&) = delete;
69  swap(*this, other);
70  }
71  SafeThreadOwnerBase& operator=(SafeThreadOwnerBase&& other) noexcept {
72  swap(*this, other);
73  return *this;
74  }
76 
77  friend void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
78 
79  explicit operator bool() const;
80 
81  std::thread::native_handle_type GetNativeThreadHandle();
82 
83  void SetJoinAtExit(bool joinAtExit) { m_joinAtExit = joinAtExit; }
84 
85  protected:
86  void Start(std::shared_ptr<SafeThread> thr);
87  std::shared_ptr<SafeThread> GetThreadSharedPtr() const;
88 
89  private:
90  mutable wpi::mutex m_mutex;
91  std::thread m_stdThread;
92  std::weak_ptr<SafeThread> m_thread;
93  std::atomic_bool m_joinAtExit{true};
94 };
95 
96 void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
97 
98 } // namespace detail
99 
100 template <typename T>
102  public:
103  template <typename... Args>
104  void Start(Args&&... args) {
105  detail::SafeThreadOwnerBase::Start(
106  std::make_shared<T>(std::forward<Args>(args)...));
107  }
108 
109  using Proxy = typename detail::SafeThreadProxy<T>;
110  Proxy GetThread() const {
111  return Proxy(detail::SafeThreadOwnerBase::GetThreadSharedPtr());
112  }
113 
114  std::shared_ptr<T> GetThreadSharedPtr() const {
115  return std::static_pointer_cast<T>(
116  detail::SafeThreadOwnerBase::GetThreadSharedPtr());
117  }
118 };
119 
120 } // namespace wpi
121 
122 #endif // WPIUTIL_WPI_SAFETHREAD_H_
wpi::SafeThreadOwner
Definition: SafeThread.h:101
wpi::detail::SafeThreadProxy
Definition: SafeThread.h:50
wpi::SafeThread
Definition: SafeThread.h:22
wpi::detail::SafeThreadProxyBase
Definition: SafeThread.h:36
wpi
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
wpi::detail::SafeThreadOwnerBase
Definition: SafeThread.h:59