2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#ifndef WPIUTIL_WPI_SAFETHREAD_H_
|
|
|
|
|
#define WPIUTIL_WPI_SAFETHREAD_H_
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
#include <atomic>
|
2018-10-05 16:32:43 -07:00
|
|
|
#include <memory>
|
2016-09-25 17:23:39 -07:00
|
|
|
#include <thread>
|
2018-10-05 16:32:43 -07:00
|
|
|
#include <utility>
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include "wpi/condition_variable.h"
|
|
|
|
|
#include "wpi/mutex.h"
|
2017-11-12 22:12:03 -08:00
|
|
|
|
2016-09-25 17:23:39 -07:00
|
|
|
namespace wpi {
|
|
|
|
|
|
2021-10-14 18:09:38 -07:00
|
|
|
/**
|
|
|
|
|
* Base class for SafeThreadOwner threads.
|
|
|
|
|
*/
|
2016-09-25 17:23:39 -07:00
|
|
|
class SafeThread {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~SafeThread() = default;
|
|
|
|
|
virtual void Main() = 0;
|
|
|
|
|
|
2018-10-05 16:32:43 -07:00
|
|
|
mutable wpi::mutex m_mutex;
|
2018-10-06 15:17:13 -07:00
|
|
|
std::atomic_bool m_active{true};
|
2017-11-12 22:12:03 -08:00
|
|
|
wpi::condition_variable m_cond;
|
2019-06-20 19:51:09 -07:00
|
|
|
std::thread::id m_threadId;
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
2021-10-14 18:09:38 -07:00
|
|
|
/**
|
|
|
|
|
* Non-template proxy base class for common proxy code.
|
|
|
|
|
*/
|
2016-09-25 17:23:39 -07:00
|
|
|
class SafeThreadProxyBase {
|
|
|
|
|
public:
|
2018-10-05 16:32:43 -07:00
|
|
|
explicit SafeThreadProxyBase(std::shared_ptr<SafeThread> thr);
|
2016-09-25 17:23:39 -07:00
|
|
|
explicit operator bool() const { return m_thread != nullptr; }
|
2017-11-12 22:12:03 -08:00
|
|
|
std::unique_lock<wpi::mutex>& GetLock() { return m_lock; }
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
protected:
|
2018-10-05 16:32:43 -07:00
|
|
|
std::shared_ptr<SafeThread> m_thread;
|
2017-11-12 22:12:03 -08:00
|
|
|
std::unique_lock<wpi::mutex> m_lock;
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
2021-10-14 18:09:38 -07:00
|
|
|
/**
|
|
|
|
|
* A proxy for SafeThread.
|
|
|
|
|
*
|
|
|
|
|
* Also serves as a scoped lock on SafeThread::m_mutex.
|
|
|
|
|
*/
|
2016-09-25 17:23:39 -07:00
|
|
|
template <typename T>
|
|
|
|
|
class SafeThreadProxy : public SafeThreadProxyBase {
|
|
|
|
|
public:
|
2018-10-05 16:32:43 -07:00
|
|
|
explicit SafeThreadProxy(std::shared_ptr<SafeThread> thr)
|
|
|
|
|
: SafeThreadProxyBase(std::move(thr)) {}
|
|
|
|
|
T& operator*() const { return *static_cast<T*>(m_thread.get()); }
|
|
|
|
|
T* operator->() const { return static_cast<T*>(m_thread.get()); }
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
2021-10-14 18:09:38 -07:00
|
|
|
/**
|
|
|
|
|
* Non-template owner base class for common owner code.
|
|
|
|
|
*/
|
2016-09-25 17:23:39 -07:00
|
|
|
class SafeThreadOwnerBase {
|
|
|
|
|
public:
|
|
|
|
|
void Stop();
|
2018-10-06 15:17:13 -07:00
|
|
|
void Join();
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-10-05 16:32:43 -07:00
|
|
|
SafeThreadOwnerBase() noexcept = default;
|
2016-09-25 17:23:39 -07:00
|
|
|
SafeThreadOwnerBase(const SafeThreadOwnerBase&) = delete;
|
|
|
|
|
SafeThreadOwnerBase& operator=(const SafeThreadOwnerBase&) = delete;
|
2018-10-05 16:32:43 -07:00
|
|
|
SafeThreadOwnerBase(SafeThreadOwnerBase&& other) noexcept
|
|
|
|
|
: SafeThreadOwnerBase() {
|
|
|
|
|
swap(*this, other);
|
|
|
|
|
}
|
2019-05-31 13:43:32 -07:00
|
|
|
SafeThreadOwnerBase& operator=(SafeThreadOwnerBase&& other) noexcept {
|
2018-10-05 16:32:43 -07:00
|
|
|
swap(*this, other);
|
2016-11-11 22:18:52 -08:00
|
|
|
return *this;
|
|
|
|
|
}
|
2018-10-06 15:17:13 -07:00
|
|
|
~SafeThreadOwnerBase();
|
2016-09-25 17:23:39 -07:00
|
|
|
|
2018-10-05 16:32:43 -07:00
|
|
|
friend void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
|
|
|
|
|
|
|
|
|
|
explicit operator bool() const;
|
|
|
|
|
|
2018-10-06 15:17:13 -07:00
|
|
|
std::thread::native_handle_type GetNativeThreadHandle();
|
|
|
|
|
|
|
|
|
|
void SetJoinAtExit(bool joinAtExit) { m_joinAtExit = joinAtExit; }
|
2016-11-11 22:18:52 -08:00
|
|
|
|
|
|
|
|
protected:
|
2018-10-05 16:32:43 -07:00
|
|
|
void Start(std::shared_ptr<SafeThread> thr);
|
2019-06-20 19:51:09 -07:00
|
|
|
std::shared_ptr<SafeThread> GetThreadSharedPtr() const;
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
private:
|
2018-10-05 16:32:43 -07:00
|
|
|
mutable wpi::mutex m_mutex;
|
2018-10-06 15:17:13 -07:00
|
|
|
std::thread m_stdThread;
|
2018-10-05 16:32:43 -07:00
|
|
|
std::weak_ptr<SafeThread> m_thread;
|
2018-10-06 15:17:13 -07:00
|
|
|
std::atomic_bool m_joinAtExit{true};
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
2018-10-05 16:32:43 -07:00
|
|
|
void swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept;
|
2016-09-25 18:21:29 -07:00
|
|
|
|
2016-09-25 17:23:39 -07:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
class SafeThreadOwner : public detail::SafeThreadOwnerBase {
|
|
|
|
|
public:
|
2018-10-05 16:32:43 -07:00
|
|
|
template <typename... Args>
|
|
|
|
|
void Start(Args&&... args) {
|
|
|
|
|
detail::SafeThreadOwnerBase::Start(
|
|
|
|
|
std::make_shared<T>(std::forward<Args>(args)...));
|
|
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
|
|
|
|
|
using Proxy = typename detail::SafeThreadProxy<T>;
|
2016-11-11 22:18:52 -08:00
|
|
|
Proxy GetThread() const {
|
2019-06-20 19:51:09 -07:00
|
|
|
return Proxy(detail::SafeThreadOwnerBase::GetThreadSharedPtr());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<T> GetThreadSharedPtr() const {
|
|
|
|
|
return std::static_pointer_cast<T>(
|
|
|
|
|
detail::SafeThreadOwnerBase::GetThreadSharedPtr());
|
2016-11-11 22:18:52 -08:00
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace wpi
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#endif // WPIUTIL_WPI_SAFETHREAD_H_
|