WPILibC++  2018.4.1-1224-g18c8cce
 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  SafeThread() { m_active = true; }
25  virtual ~SafeThread() = default;
26  virtual void Main() = 0;
27 
28  mutable wpi::mutex m_mutex;
29  std::atomic_bool m_active;
30  wpi::condition_variable m_cond;
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 
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  }
74  ~SafeThreadOwnerBase() { Stop(); }
75 
76  friend void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
77 
78  explicit operator bool() const;
79 
80  std::thread::native_handle_type GetNativeThreadHandle() const;
81 
82  protected:
83  void Start(std::shared_ptr<SafeThread> thr);
84  std::shared_ptr<SafeThread> GetThread() const;
85 
86  private:
87  mutable wpi::mutex m_mutex;
88  std::weak_ptr<SafeThread> m_thread;
89  std::thread::native_handle_type m_nativeHandle;
90 };
91 
92 void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
93 
94 } // namespace detail
95 
96 template <typename T>
98  public:
99  template <typename... Args>
100  void Start(Args&&... args) {
101  detail::SafeThreadOwnerBase::Start(
102  std::make_shared<T>(std::forward<Args>(args)...));
103  }
104 
105  using Proxy = typename detail::SafeThreadProxy<T>;
106  Proxy GetThread() const {
107  return Proxy(detail::SafeThreadOwnerBase::GetThread());
108  }
109 };
110 
111 } // namespace wpi
112 
113 #endif // WPIUTIL_WPI_SAFETHREAD_H_
Definition: SafeThread.h:50
Definition: SafeThread.h:22
Definition: SafeThread.h:97
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
Definition: SafeThread.h:59
Definition: SafeThread.h:36