blob: bbecc5c232e17efab8f76fba84693b72bc91a997 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "wpi/SafeThread.h"
6
7using namespace wpi;
8
9detail::SafeThreadProxyBase::SafeThreadProxyBase(
10 std::shared_ptr<SafeThread> thr)
11 : m_thread(std::move(thr)) {
Austin Schuh812d0d12021-11-04 20:16:48 -070012 if (!m_thread) {
13 return;
14 }
Brian Silverman8fce7482020-01-05 13:18:21 -080015 m_lock = std::unique_lock<wpi::mutex>(m_thread->m_mutex);
16 if (!m_thread->m_active) {
17 m_lock.unlock();
18 m_thread = nullptr;
19 return;
20 }
21}
22
23detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() {
Austin Schuh812d0d12021-11-04 20:16:48 -070024 if (m_joinAtExit) {
Brian Silverman8fce7482020-01-05 13:18:21 -080025 Join();
Austin Schuh812d0d12021-11-04 20:16:48 -070026 } else {
Brian Silverman8fce7482020-01-05 13:18:21 -080027 Stop();
Austin Schuh812d0d12021-11-04 20:16:48 -070028 }
Brian Silverman8fce7482020-01-05 13:18:21 -080029}
30
31void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) {
32 std::scoped_lock lock(m_mutex);
Austin Schuh812d0d12021-11-04 20:16:48 -070033 if (auto thr = m_thread.lock()) {
34 return;
35 }
Brian Silverman8fce7482020-01-05 13:18:21 -080036 m_stdThread = std::thread([=] { thr->Main(); });
37 thr->m_threadId = m_stdThread.get_id();
38 m_thread = thr;
39}
40
41void detail::SafeThreadOwnerBase::Stop() {
42 std::scoped_lock lock(m_mutex);
43 if (auto thr = m_thread.lock()) {
44 thr->m_active = false;
45 thr->m_cond.notify_all();
46 m_thread.reset();
47 }
Austin Schuh812d0d12021-11-04 20:16:48 -070048 if (m_stdThread.joinable()) {
49 m_stdThread.detach();
50 }
Brian Silverman8fce7482020-01-05 13:18:21 -080051}
52
53void detail::SafeThreadOwnerBase::Join() {
54 std::unique_lock lock(m_mutex);
55 if (auto thr = m_thread.lock()) {
56 auto stdThread = std::move(m_stdThread);
57 m_thread.reset();
58 lock.unlock();
59 thr->m_active = false;
60 thr->m_cond.notify_all();
61 stdThread.join();
62 } else if (m_stdThread.joinable()) {
63 m_stdThread.detach();
64 }
65}
66
67void detail::swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept {
68 using std::swap;
Austin Schuh812d0d12021-11-04 20:16:48 -070069 if (&lhs == &rhs) {
70 return;
71 }
Brian Silverman8fce7482020-01-05 13:18:21 -080072 std::scoped_lock lock(lhs.m_mutex, rhs.m_mutex);
73 std::swap(lhs.m_stdThread, rhs.m_stdThread);
74 std::swap(lhs.m_thread, rhs.m_thread);
75}
76
77detail::SafeThreadOwnerBase::operator bool() const {
78 std::scoped_lock lock(m_mutex);
79 return !m_thread.expired();
80}
81
82std::thread::native_handle_type
83detail::SafeThreadOwnerBase::GetNativeThreadHandle() {
84 std::scoped_lock lock(m_mutex);
85 return m_stdThread.native_handle();
86}
87
88std::shared_ptr<SafeThread> detail::SafeThreadOwnerBase::GetThreadSharedPtr()
89 const {
90 std::scoped_lock lock(m_mutex);
91 return m_thread.lock();
92}