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.
|
2018-10-05 16:32:43 -07:00
|
|
|
|
|
|
|
|
#include "wpi/SafeThread.h"
|
|
|
|
|
|
2022-12-31 18:40:45 -05:00
|
|
|
#include <atomic>
|
|
|
|
|
|
2018-10-05 16:32:43 -07:00
|
|
|
using namespace wpi;
|
|
|
|
|
|
2022-12-31 18:40:45 -05:00
|
|
|
// thread start/stop notifications for bindings that need to set up
|
|
|
|
|
// per-thread state
|
|
|
|
|
|
|
|
|
|
static void* DefaultOnThreadStart() {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
static void DefaultOnThreadEnd(void*) {}
|
|
|
|
|
|
|
|
|
|
using OnThreadStartFn = void* (*)();
|
|
|
|
|
using OnThreadEndFn = void (*)(void*);
|
|
|
|
|
static std::atomic<int> gSafeThreadRefcount;
|
|
|
|
|
static std::atomic<OnThreadStartFn> gOnSafeThreadStart{DefaultOnThreadStart};
|
|
|
|
|
static std::atomic<OnThreadEndFn> gOnSafeThreadEnd{DefaultOnThreadEnd};
|
|
|
|
|
|
|
|
|
|
namespace wpi::impl {
|
|
|
|
|
void SetSafeThreadNotifiers(OnThreadStartFn OnStart, OnThreadEndFn OnEnd) {
|
|
|
|
|
if (gSafeThreadRefcount != 0) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
"cannot set notifier while safe threads are running");
|
|
|
|
|
}
|
|
|
|
|
// Note: there's a race here, but if you're not calling this function on
|
|
|
|
|
// the main thread before you start anything else, you're using this function
|
|
|
|
|
// incorrectly
|
|
|
|
|
gOnSafeThreadStart = OnStart ? OnStart : DefaultOnThreadStart;
|
|
|
|
|
gOnSafeThreadEnd = OnEnd ? OnEnd : DefaultOnThreadEnd;
|
|
|
|
|
}
|
|
|
|
|
} // namespace wpi::impl
|
|
|
|
|
|
2022-08-31 11:29:57 -07:00
|
|
|
void SafeThread::Stop() {
|
|
|
|
|
m_active = false;
|
|
|
|
|
m_cond.notify_all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SafeThreadEvent::Stop() {
|
|
|
|
|
m_active = false;
|
|
|
|
|
m_stopEvent.Set();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-05 16:32:43 -07:00
|
|
|
detail::SafeThreadProxyBase::SafeThreadProxyBase(
|
2022-08-31 11:29:57 -07:00
|
|
|
std::shared_ptr<SafeThreadBase> thr)
|
2018-10-05 16:32:43 -07:00
|
|
|
: m_thread(std::move(thr)) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (!m_thread) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-05 16:32:43 -07:00
|
|
|
m_lock = std::unique_lock<wpi::mutex>(m_thread->m_mutex);
|
|
|
|
|
if (!m_thread->m_active) {
|
|
|
|
|
m_lock.unlock();
|
|
|
|
|
m_thread = nullptr;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-06 15:17:13 -07:00
|
|
|
detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_joinAtExit) {
|
2018-10-06 15:17:13 -07:00
|
|
|
Join();
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2018-10-06 15:17:13 -07:00
|
|
|
Stop();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-10-06 15:17:13 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 11:29:57 -07:00
|
|
|
void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThreadBase> thr) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (auto thr = m_thread.lock()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-31 18:40:45 -05:00
|
|
|
m_stdThread = std::thread([=] {
|
|
|
|
|
gSafeThreadRefcount++;
|
|
|
|
|
void* opaque = (gOnSafeThreadStart.load())();
|
|
|
|
|
thr->Main();
|
|
|
|
|
(gOnSafeThreadEnd.load())(opaque);
|
|
|
|
|
gSafeThreadRefcount--;
|
|
|
|
|
});
|
2019-06-20 19:51:09 -07:00
|
|
|
thr->m_threadId = m_stdThread.get_id();
|
2018-10-05 16:32:43 -07:00
|
|
|
m_thread = thr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void detail::SafeThreadOwnerBase::Stop() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2018-10-05 16:32:43 -07:00
|
|
|
if (auto thr = m_thread.lock()) {
|
2022-08-31 11:29:57 -07:00
|
|
|
thr->Stop();
|
2018-10-06 15:17:13 -07:00
|
|
|
m_thread.reset();
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_stdThread.joinable()) {
|
|
|
|
|
m_stdThread.detach();
|
|
|
|
|
}
|
2018-10-06 15:17:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void detail::SafeThreadOwnerBase::Join() {
|
2019-07-07 19:17:14 -07:00
|
|
|
std::unique_lock lock(m_mutex);
|
2018-10-06 15:17:13 -07:00
|
|
|
if (auto thr = m_thread.lock()) {
|
|
|
|
|
auto stdThread = std::move(m_stdThread);
|
|
|
|
|
m_thread.reset();
|
|
|
|
|
lock.unlock();
|
2022-08-31 11:29:57 -07:00
|
|
|
thr->Stop();
|
2018-10-06 15:17:13 -07:00
|
|
|
stdThread.join();
|
2018-10-06 18:07:56 -07:00
|
|
|
} else if (m_stdThread.joinable()) {
|
|
|
|
|
m_stdThread.detach();
|
2018-10-05 16:32:43 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void detail::swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept {
|
|
|
|
|
using std::swap;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (&lhs == &rhs) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(lhs.m_mutex, rhs.m_mutex);
|
2018-10-06 15:17:13 -07:00
|
|
|
std::swap(lhs.m_stdThread, rhs.m_stdThread);
|
2018-10-05 16:32:43 -07:00
|
|
|
std::swap(lhs.m_thread, rhs.m_thread);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
detail::SafeThreadOwnerBase::operator bool() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2018-10-05 16:32:43 -07:00
|
|
|
return !m_thread.expired();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::thread::native_handle_type
|
2018-10-06 15:17:13 -07:00
|
|
|
detail::SafeThreadOwnerBase::GetNativeThreadHandle() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2018-10-06 15:17:13 -07:00
|
|
|
return m_stdThread.native_handle();
|
2018-10-05 16:32:43 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 11:29:57 -07:00
|
|
|
std::shared_ptr<SafeThreadBase>
|
|
|
|
|
detail::SafeThreadOwnerBase::GetThreadSharedPtr() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_mutex);
|
2018-10-05 16:32:43 -07:00
|
|
|
return m_thread.lock();
|
|
|
|
|
}
|