WPILibC++  2019.2.1-19-g8a9822a
 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 <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 };
31 
32 namespace detail {
33 
34 // Non-template proxy base class for common proxy code.
36  public:
37  explicit SafeThreadProxyBase(std::shared_ptr<SafeThread> thr);
38  explicit operator bool() const { return m_thread != nullptr; }
39  std::unique_lock<wpi::mutex>& GetLock() { return m_lock; }
40 
41  protected:
42  std::shared_ptr<SafeThread> m_thread;
43  std::unique_lock<wpi::mutex> m_lock;
44 };
45 
46 // A proxy for SafeThread.
47 // Also serves as a scoped lock on SafeThread::m_mutex.
48 template <typename T>
50  public:
51  explicit SafeThreadProxy(std::shared_ptr<SafeThread> thr)
52  : SafeThreadProxyBase(std::move(thr)) {}
53  T& operator*() const { return *static_cast<T*>(m_thread.get()); }
54  T* operator->() const { return static_cast<T*>(m_thread.get()); }
55 };
56 
57 // Non-template owner base class for common owner code.
59  public:
60  void Stop();
61  void Join();
62 
63  SafeThreadOwnerBase() noexcept = default;
65  SafeThreadOwnerBase& operator=(const SafeThreadOwnerBase&) = delete;
68  swap(*this, other);
69  }
70  SafeThreadOwnerBase& operator=(SafeThreadOwnerBase other) noexcept {
71  swap(*this, other);
72  return *this;
73  }
75 
76  friend void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
77 
78  explicit operator bool() const;
79 
80  std::thread::native_handle_type GetNativeThreadHandle();
81 
82  void SetJoinAtExit(bool joinAtExit) { m_joinAtExit = joinAtExit; }
83 
84  protected:
85  void Start(std::shared_ptr<SafeThread> thr);
86  std::shared_ptr<SafeThread> GetThread() const;
87 
88  private:
89  mutable wpi::mutex m_mutex;
90  std::thread m_stdThread;
91  std::weak_ptr<SafeThread> m_thread;
92  std::atomic_bool m_joinAtExit{true};
93 };
94 
95 void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
96 
97 } // namespace detail
98 
99 template <typename T>
101  public:
102  template <typename... Args>
103  void Start(Args&&... args) {
104  detail::SafeThreadOwnerBase::Start(
105  std::make_shared<T>(std::forward<Args>(args)...));
106  }
107 
108  using Proxy = typename detail::SafeThreadProxy<T>;
109  Proxy GetThread() const {
110  return Proxy(detail::SafeThreadOwnerBase::GetThread());
111  }
112 };
113 
114 } // namespace wpi
115 
116 #endif // WPIUTIL_WPI_SAFETHREAD_H_
Definition: SafeThread.h:49
Definition: SafeThread.h:22
Definition: SafeThread.h:100
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: SafeThread.h:58
Definition: SafeThread.h:35