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 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 7 | #include <atomic> |
| 8 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 9 | using namespace wpi; |
| 10 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 11 | // thread start/stop notifications for bindings that need to set up |
| 12 | // per-thread state |
| 13 | |
| 14 | static void* DefaultOnThreadStart() { |
| 15 | return nullptr; |
| 16 | } |
| 17 | static void DefaultOnThreadEnd(void*) {} |
| 18 | |
| 19 | using OnThreadStartFn = void* (*)(); |
| 20 | using OnThreadEndFn = void (*)(void*); |
| 21 | static std::atomic<int> gSafeThreadRefcount; |
| 22 | static std::atomic<OnThreadStartFn> gOnSafeThreadStart{DefaultOnThreadStart}; |
| 23 | static std::atomic<OnThreadEndFn> gOnSafeThreadEnd{DefaultOnThreadEnd}; |
| 24 | |
| 25 | namespace wpi::impl { |
| 26 | void SetSafeThreadNotifiers(OnThreadStartFn OnStart, OnThreadEndFn OnEnd) { |
| 27 | if (gSafeThreadRefcount != 0) { |
| 28 | throw std::runtime_error( |
| 29 | "cannot set notifier while safe threads are running"); |
| 30 | } |
| 31 | // Note: there's a race here, but if you're not calling this function on |
| 32 | // the main thread before you start anything else, you're using this function |
| 33 | // incorrectly |
| 34 | gOnSafeThreadStart = OnStart ? OnStart : DefaultOnThreadStart; |
| 35 | gOnSafeThreadEnd = OnEnd ? OnEnd : DefaultOnThreadEnd; |
| 36 | } |
| 37 | } // namespace wpi::impl |
| 38 | |
| 39 | void SafeThread::Stop() { |
| 40 | m_active = false; |
| 41 | m_cond.notify_all(); |
| 42 | } |
| 43 | |
| 44 | void SafeThreadEvent::Stop() { |
| 45 | m_active = false; |
| 46 | m_stopEvent.Set(); |
| 47 | } |
| 48 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 49 | detail::SafeThreadProxyBase::SafeThreadProxyBase( |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 50 | std::shared_ptr<SafeThreadBase> thr) |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 51 | : m_thread(std::move(thr)) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 52 | if (!m_thread) { |
| 53 | return; |
| 54 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 55 | m_lock = std::unique_lock<wpi::mutex>(m_thread->m_mutex); |
| 56 | if (!m_thread->m_active) { |
| 57 | m_lock.unlock(); |
| 58 | m_thread = nullptr; |
| 59 | return; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | detail::SafeThreadOwnerBase::~SafeThreadOwnerBase() { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 64 | if (m_joinAtExit) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | Join(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | } else { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | Stop(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 68 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 69 | } |
| 70 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 71 | void detail::SafeThreadOwnerBase::Start(std::shared_ptr<SafeThreadBase> thr) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 72 | std::scoped_lock lock(m_mutex); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 73 | if (auto thr = m_thread.lock()) { |
| 74 | return; |
| 75 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 76 | m_stdThread = std::thread([=] { |
| 77 | gSafeThreadRefcount++; |
| 78 | void* opaque = (gOnSafeThreadStart.load())(); |
| 79 | thr->Main(); |
| 80 | (gOnSafeThreadEnd.load())(opaque); |
| 81 | gSafeThreadRefcount--; |
| 82 | }); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | thr->m_threadId = m_stdThread.get_id(); |
| 84 | m_thread = thr; |
| 85 | } |
| 86 | |
| 87 | void detail::SafeThreadOwnerBase::Stop() { |
| 88 | std::scoped_lock lock(m_mutex); |
| 89 | if (auto thr = m_thread.lock()) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 90 | thr->Stop(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 91 | m_thread.reset(); |
| 92 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 93 | if (m_stdThread.joinable()) { |
| 94 | m_stdThread.detach(); |
| 95 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void detail::SafeThreadOwnerBase::Join() { |
| 99 | std::unique_lock lock(m_mutex); |
| 100 | if (auto thr = m_thread.lock()) { |
| 101 | auto stdThread = std::move(m_stdThread); |
| 102 | m_thread.reset(); |
| 103 | lock.unlock(); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 104 | thr->Stop(); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 105 | stdThread.join(); |
| 106 | } else if (m_stdThread.joinable()) { |
| 107 | m_stdThread.detach(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void detail::swap(SafeThreadOwnerBase& lhs, SafeThreadOwnerBase& rhs) noexcept { |
| 112 | using std::swap; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 113 | if (&lhs == &rhs) { |
| 114 | return; |
| 115 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 116 | std::scoped_lock lock(lhs.m_mutex, rhs.m_mutex); |
| 117 | std::swap(lhs.m_stdThread, rhs.m_stdThread); |
| 118 | std::swap(lhs.m_thread, rhs.m_thread); |
| 119 | } |
| 120 | |
| 121 | detail::SafeThreadOwnerBase::operator bool() const { |
| 122 | std::scoped_lock lock(m_mutex); |
| 123 | return !m_thread.expired(); |
| 124 | } |
| 125 | |
| 126 | std::thread::native_handle_type |
| 127 | detail::SafeThreadOwnerBase::GetNativeThreadHandle() { |
| 128 | std::scoped_lock lock(m_mutex); |
| 129 | return m_stdThread.native_handle(); |
| 130 | } |
| 131 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 132 | std::shared_ptr<SafeThreadBase> |
| 133 | detail::SafeThreadOwnerBase::GetThreadSharedPtr() const { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 134 | std::scoped_lock lock(m_mutex); |
| 135 | return m_thread.lock(); |
| 136 | } |