Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
Make SafeThreadOwner moveable. (#153)
Browse files Browse the repository at this point in the history
This allows it to be used in data structures such as std::vector.

Also make GetThread() const.
  • Loading branch information
PeterJohnson authored Nov 12, 2016
1 parent dc94a3f commit c23880f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions wpiutil/include/support/SafeThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,24 @@ class SafeThreadOwnerBase {
public:
void Stop();

protected:
SafeThreadOwnerBase() { m_thread = nullptr; }
SafeThreadOwnerBase(const SafeThreadOwnerBase&) = delete;
SafeThreadOwnerBase& operator=(const SafeThreadOwnerBase&) = delete;
SafeThreadOwnerBase(SafeThreadOwnerBase&& other)
: m_thread(other.m_thread.exchange(nullptr)) {}
SafeThreadOwnerBase& operator=(SafeThreadOwnerBase other) {
SafeThread* otherthr = other.m_thread.exchange(nullptr);
SafeThread* curthr = m_thread.exchange(otherthr);
other.m_thread.exchange(curthr); // other destructor will clean up
return *this;
}
~SafeThreadOwnerBase() { Stop(); }

explicit operator bool() const { return m_thread.load(); }

protected:
void Start(SafeThread* thr);
SafeThread* GetThread() { return m_thread.load(); }
SafeThread* GetThread() const { return m_thread.load(); }

private:
std::atomic<SafeThread*> m_thread;
Expand Down Expand Up @@ -106,7 +116,9 @@ class SafeThreadOwner : public detail::SafeThreadOwnerBase {
void Start(T* thr) { detail::SafeThreadOwnerBase::Start(thr); }

using Proxy = typename detail::SafeThreadProxy<T>;
Proxy GetThread() { return Proxy(detail::SafeThreadOwnerBase::GetThread()); }
Proxy GetThread() const {
return Proxy(detail::SafeThreadOwnerBase::GetThread());
}
};

} // namespace wpi
Expand Down

0 comments on commit c23880f

Please sign in to comment.