WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SafeThread.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <atomic>
8#include <memory>
9#include <thread>
10#include <utility>
11
14#include "wpi/util/mutex.hpp"
15
16namespace wpi::util {
17
18/**
19 * Base class for SafeThreadOwner threads.
20 */
22 public:
23 virtual ~SafeThreadBase() = default;
24 virtual void Main() = 0;
25 virtual void Stop() = 0;
26
28 std::atomic_bool m_active{true};
29 std::thread::id m_threadId;
30};
31
32class SafeThread : public SafeThreadBase {
33 public:
34 void Stop() override;
35
37};
38
40 public:
42
43 void Stop() override;
44
46};
47
48namespace detail {
49
50/**
51 * Non-template proxy base class for common proxy code.
52 */
54 public:
55 explicit SafeThreadProxyBase(std::shared_ptr<SafeThreadBase> thr);
56 explicit operator bool() const { return m_thread != nullptr; }
57 std::unique_lock<wpi::util::mutex>& GetLock() { return m_lock; }
58
59 protected:
60 std::shared_ptr<SafeThreadBase> m_thread;
61 std::unique_lock<wpi::util::mutex> m_lock;
62};
63
64/**
65 * A proxy for SafeThread.
66 *
67 * Also serves as a scoped lock on SafeThread::m_mutex.
68 */
69template <typename T>
71 public:
72 explicit SafeThreadProxy(std::shared_ptr<SafeThreadBase> thr)
73 : SafeThreadProxyBase(std::move(thr)) {}
74 T& operator*() const { return *static_cast<T*>(m_thread.get()); }
75 T* operator->() const { return static_cast<T*>(m_thread.get()); }
76};
77
78/**
79 * Non-template owner base class for common owner code.
80 */
82 public:
83 void Stop();
84 void Join();
85
86 SafeThreadOwnerBase() noexcept = default;
88 SafeThreadOwnerBase& operator=(const SafeThreadOwnerBase&) = delete;
91 swap(*this, other);
92 }
94 swap(*this, other);
95 return *this;
96 }
98
99 friend void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
100
101 explicit operator bool() const;
102
103 std::thread::native_handle_type GetNativeThreadHandle();
104
105 void SetJoinAtExit(bool joinAtExit) { m_joinAtExit = joinAtExit; }
106
107 protected:
108 void Start(std::shared_ptr<SafeThreadBase> thr);
109 std::shared_ptr<SafeThreadBase> GetThreadSharedPtr() const;
110
111 private:
112 mutable wpi::util::mutex m_mutex;
113 std::thread m_stdThread;
114 std::weak_ptr<SafeThreadBase> m_thread;
115 std::atomic_bool m_joinAtExit{true};
116};
117
119
120} // namespace detail
121
122template <typename T>
124 public:
125 template <typename... Args>
126 void Start(Args&&... args) {
128 std::make_shared<T>(std::forward<Args>(args)...));
129 }
130
135
136 std::shared_ptr<T> GetThreadSharedPtr() const {
137 return std::static_pointer_cast<T>(
139 }
140};
141
142} // namespace wpi::util
An atomic signaling event for synchronization.
Definition Synchronization.hpp:231
Base class for SafeThreadOwner threads.
Definition SafeThread.hpp:21
virtual void Stop()=0
virtual void Main()=0
std::thread::id m_threadId
Definition SafeThread.hpp:29
wpi::util::mutex m_mutex
Definition SafeThread.hpp:27
virtual ~SafeThreadBase()=default
std::atomic_bool m_active
Definition SafeThread.hpp:28
Event m_stopEvent
Definition SafeThread.hpp:45
SafeThreadEvent()
Definition SafeThread.hpp:41
Definition SafeThread.hpp:32
void Stop() override
wpi::util::condition_variable m_cond
Definition SafeThread.hpp:36
Definition SafeThread.hpp:123
void Start(Args &&... args)
Definition SafeThread.hpp:126
typename detail::SafeThreadProxy< T > Proxy
Definition SafeThread.hpp:131
Proxy GetThread() const
Definition SafeThread.hpp:132
std::shared_ptr< T > GetThreadSharedPtr() const
Definition SafeThread.hpp:136
Non-template owner base class for common owner code.
Definition SafeThread.hpp:81
void SetJoinAtExit(bool joinAtExit)
Definition SafeThread.hpp:105
std::thread::native_handle_type GetNativeThreadHandle()
friend void swap(SafeThreadOwnerBase &lhs, SafeThreadOwnerBase &rhs) noexcept
void Start(std::shared_ptr< SafeThreadBase > thr)
SafeThreadOwnerBase & operator=(SafeThreadOwnerBase &&other) noexcept
Definition SafeThread.hpp:93
std::shared_ptr< SafeThreadBase > GetThreadSharedPtr() const
std::shared_ptr< SafeThreadBase > m_thread
Definition SafeThread.hpp:60
std::unique_lock< wpi::util::mutex > m_lock
Definition SafeThread.hpp:61
std::unique_lock< wpi::util::mutex > & GetLock()
Definition SafeThread.hpp:57
SafeThreadProxyBase(std::shared_ptr< SafeThreadBase > thr)
A proxy for SafeThread.
Definition SafeThread.hpp:70
SafeThreadProxy(std::shared_ptr< SafeThreadBase > thr)
Definition SafeThread.hpp:72
T & operator*() const
Definition SafeThread.hpp:74
T * operator->() const
Definition SafeThread.hpp:75
Converts a string literal into a format string that will be parsed at compile time and converted into...
Definition printf.h:50
Definition StringMap.hpp:773
void swap(SafeThreadOwnerBase &lhs, SafeThreadOwnerBase &rhs) noexcept
Definition raw_os_ostream.hpp:19
::std::condition_variable condition_variable
Definition condition_variable.hpp:16
::std::mutex mutex
Definition mutex.hpp:17