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