Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 1 | // 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 Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "wpi/SafeThread.h" |
| 6 | |
| 7 | using namespace wpi; |
| 8 | |
| 9 | detail::SafeThreadProxyBase::SafeThreadProxyBase( |
| 10 | std::shared_ptr<SafeThread> thr) |
| 11 | : m_thread(std::move(thr)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 12 | if (!m_thread) { |
| 13 | return; |
| 14 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 15 | 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 | |
| 23 | detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 24 | if (m_joinAtExit) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | Join(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 26 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 27 | Stop(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 28 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThread> thr) { |
| 32 | std::scoped_lock lock(m_mutex); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 33 | if (auto thr = m_thread.lock()) { |
| 34 | return; |
| 35 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | m_stdThread = std::thread([=] { thr->Main(); }); |
| 37 | thr->m_threadId = m_stdThread.get_id(); |
| 38 | m_thread = thr; |
| 39 | } |
| 40 | |
| 41 | void 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 Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 48 | if (m_stdThread.joinable()) { |
| 49 | m_stdThread.detach(); |
| 50 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void 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 | |
| 67 | void detail::swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept { |
| 68 | using std::swap; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 69 | if (&lhs == &rhs) { |
| 70 | return; |
| 71 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 72 | 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 | |
| 77 | detail::SafeThreadOwnerBase::operator bool() const { |
| 78 | std::scoped_lock lock(m_mutex); |
| 79 | return !m_thread.expired(); |
| 80 | } |
| 81 | |
| 82 | std::thread::native_handle_type |
| 83 | detail::SafeThreadOwnerBase::GetNativeThreadHandle() { |
| 84 | std::scoped_lock lock(m_mutex); |
| 85 | return m_stdThread.native_handle(); |
| 86 | } |
| 87 | |
| 88 | std::shared_ptr<SafeThread> detail::SafeThreadOwnerBase::GetThreadSharedPtr() |
| 89 | const { |
| 90 | std::scoped_lock lock(m_mutex); |
| 91 | return m_thread.lock(); |
| 92 | } |