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 "frc/Notifier.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 9 | #include <fmt/format.h> |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 10 | #include <hal/DriverStation.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 11 | #include <hal/FRCUsageReporting.h> |
| 12 | #include <hal/Notifier.h> |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 13 | #include <hal/Threads.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 15 | #include "frc/Errors.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | #include "frc/Timer.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 17 | |
| 18 | using namespace frc; |
| 19 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 20 | Notifier::Notifier(std::function<void()> callback) { |
| 21 | if (!callback) { |
| 22 | throw FRC_MakeError(err::NullParameter, "callback"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 23 | } |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 24 | m_callback = callback; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | int32_t status = 0; |
| 26 | m_notifier = HAL_InitializeNotifier(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 27 | FRC_CheckErrorStatus(status, "InitializeNotifier"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 28 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 29 | m_thread = std::thread([=, this] { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 30 | for (;;) { |
| 31 | int32_t status = 0; |
| 32 | HAL_NotifierHandle notifier = m_notifier.load(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 33 | if (notifier == 0) { |
| 34 | break; |
| 35 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 37 | if (curTime == 0 || status != 0) { |
| 38 | break; |
| 39 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 41 | std::function<void()> callback; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 42 | { |
| 43 | std::scoped_lock lock(m_processMutex); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 44 | callback = m_callback; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 45 | if (m_periodic) { |
| 46 | m_expirationTime += m_period; |
| 47 | UpdateAlarm(); |
| 48 | } else { |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 49 | // Need to update the alarm to cause it to wait again |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | UpdateAlarm(UINT64_MAX); |
| 51 | } |
| 52 | } |
| 53 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 54 | // Call callback |
| 55 | if (callback) { |
| 56 | callback(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 57 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 58 | } |
| 59 | }); |
| 60 | } |
| 61 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 62 | Notifier::Notifier(int priority, std::function<void()> callback) { |
| 63 | if (!callback) { |
| 64 | throw FRC_MakeError(err::NullParameter, "callback"); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 65 | } |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 66 | m_callback = callback; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 67 | int32_t status = 0; |
| 68 | m_notifier = HAL_InitializeNotifier(&status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 69 | FRC_CheckErrorStatus(status, "InitializeNotifier"); |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 70 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 71 | m_thread = std::thread([=, this] { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 72 | int32_t status = 0; |
| 73 | HAL_SetCurrentThreadPriority(true, priority, &status); |
| 74 | for (;;) { |
| 75 | HAL_NotifierHandle notifier = m_notifier.load(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 76 | if (notifier == 0) { |
| 77 | break; |
| 78 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 79 | uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 80 | if (curTime == 0 || status != 0) { |
| 81 | break; |
| 82 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 83 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 84 | std::function<void()> callback; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 85 | { |
| 86 | std::scoped_lock lock(m_processMutex); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 87 | callback = m_callback; |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 88 | if (m_periodic) { |
| 89 | m_expirationTime += m_period; |
| 90 | UpdateAlarm(); |
| 91 | } else { |
| 92 | // need to update the alarm to cause it to wait again |
| 93 | UpdateAlarm(UINT64_MAX); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // call callback |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 98 | if (callback) { |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 99 | try { |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 100 | callback(); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 101 | } catch (const frc::RuntimeError& e) { |
| 102 | e.Report(); |
| 103 | FRC_ReportError( |
| 104 | err::Error, |
| 105 | "Error in Notifier thread." |
| 106 | " The above stacktrace can help determine where the error " |
| 107 | "occurred.\n" |
| 108 | " See https://wpilib.org/stacktrace for more information.\n"); |
| 109 | throw; |
| 110 | } catch (const std::exception& e) { |
| 111 | HAL_SendError(1, err::Error, 0, e.what(), "", "", 1); |
| 112 | throw; |
| 113 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 114 | } |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 115 | } |
| 116 | }); |
| 117 | } |
| 118 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | Notifier::~Notifier() { |
| 120 | int32_t status = 0; |
| 121 | // atomically set handle to 0, then clean |
| 122 | HAL_NotifierHandle handle = m_notifier.exchange(0); |
| 123 | HAL_StopNotifier(handle, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 124 | FRC_ReportError(status, "StopNotifier"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 126 | // Join the thread to ensure the callback has exited. |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 127 | if (m_thread.joinable()) { |
| 128 | m_thread.join(); |
| 129 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 130 | |
| 131 | HAL_CleanNotifier(handle, &status); |
| 132 | } |
| 133 | |
| 134 | Notifier::Notifier(Notifier&& rhs) |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 135 | : m_thread(std::move(rhs.m_thread)), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 136 | m_notifier(rhs.m_notifier.load()), |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 137 | m_callback(std::move(rhs.m_callback)), |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 138 | m_expirationTime(std::move(rhs.m_expirationTime)), |
| 139 | m_period(std::move(rhs.m_period)), |
| 140 | m_periodic(std::move(rhs.m_periodic)) { |
| 141 | rhs.m_notifier = HAL_kInvalidHandle; |
| 142 | } |
| 143 | |
| 144 | Notifier& Notifier::operator=(Notifier&& rhs) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 145 | m_thread = std::move(rhs.m_thread); |
| 146 | m_notifier = rhs.m_notifier.load(); |
| 147 | rhs.m_notifier = HAL_kInvalidHandle; |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 148 | m_callback = std::move(rhs.m_callback); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 149 | m_expirationTime = std::move(rhs.m_expirationTime); |
| 150 | m_period = std::move(rhs.m_period); |
| 151 | m_periodic = std::move(rhs.m_periodic); |
| 152 | |
| 153 | return *this; |
| 154 | } |
| 155 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 156 | void Notifier::SetName(std::string_view name) { |
| 157 | fmt::memory_buffer buf; |
| 158 | fmt::format_to(fmt::appender{buf}, "{}", name); |
| 159 | buf.push_back('\0'); // null terminate |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 160 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 161 | HAL_SetNotifierName(m_notifier, buf.data(), &status); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 162 | } |
| 163 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 164 | void Notifier::SetHandler(std::function<void()> callback) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 165 | std::scoped_lock lock(m_processMutex); |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 166 | m_callback = callback; |
| 167 | } |
| 168 | |
| 169 | void Notifier::SetCallback(std::function<void()> callback) { |
| 170 | std::scoped_lock lock(m_processMutex); |
| 171 | m_callback = callback; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 174 | void Notifier::StartSingle(units::second_t delay) { |
| 175 | std::scoped_lock lock(m_processMutex); |
| 176 | m_periodic = false; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 177 | m_period = delay; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 178 | m_expirationTime = Timer::GetFPGATimestamp() + m_period; |
| 179 | UpdateAlarm(); |
| 180 | } |
| 181 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 182 | void Notifier::StartPeriodic(units::second_t period) { |
| 183 | std::scoped_lock lock(m_processMutex); |
| 184 | m_periodic = true; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 185 | m_period = period; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 186 | m_expirationTime = Timer::GetFPGATimestamp() + m_period; |
| 187 | UpdateAlarm(); |
| 188 | } |
| 189 | |
| 190 | void Notifier::Stop() { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 191 | std::scoped_lock lock(m_processMutex); |
| 192 | m_periodic = false; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 193 | int32_t status = 0; |
| 194 | HAL_CancelNotifierAlarm(m_notifier, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 195 | FRC_CheckErrorStatus(status, "CancelNotifierAlarm"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void Notifier::UpdateAlarm(uint64_t triggerTime) { |
| 199 | int32_t status = 0; |
| 200 | // Return if we are being destructed, or were not created successfully |
| 201 | auto notifier = m_notifier.load(); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 202 | if (notifier == 0) { |
| 203 | return; |
| 204 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 205 | HAL_UpdateNotifierAlarm(notifier, triggerTime, &status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 206 | FRC_CheckErrorStatus(status, "UpdateNotifierAlarm"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void Notifier::UpdateAlarm() { |
| 210 | UpdateAlarm(static_cast<uint64_t>(m_expirationTime * 1e6)); |
| 211 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 212 | |
| 213 | bool Notifier::SetHALThreadPriority(bool realTime, int32_t priority) { |
| 214 | int32_t status = 0; |
| 215 | return HAL_SetNotifierThreadPriority(realTime, priority, &status); |
| 216 | } |