2016-09-25 17:23:39 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-01 17:32:39 -08:00
|
|
|
/* Copyright (c) 2015-2018 FIRST. All Rights Reserved. */
|
2016-09-25 17:23:39 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
|
|
// Base class for SafeThreadOwner threads.
|
|
|
|
|
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;
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
// Non-template proxy base class for common proxy code.
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// A proxy for SafeThread.
|
|
|
|
|
// Also serves as a scoped lock on SafeThread::m_mutex.
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Non-template owner base class for common owner code.
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
SafeThreadOwnerBase& operator=(SafeThreadOwnerBase other) noexcept {
|
|
|
|
|
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);
|
|
|
|
|
std::shared_ptr<SafeThread> GetThread() 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 {
|
|
|
|
|
return Proxy(detail::SafeThreadOwnerBase::GetThread());
|
|
|
|
|
}
|
2016-09-25 17:23:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace wpi
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#endif // WPIUTIL_WPI_SAFETHREAD_H_
|