Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 2 | /* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */ |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/Notifier.h" |
| 9 | |
| 10 | #include <utility> |
| 11 | |
| 12 | #include <hal/HAL.h> |
| 13 | |
| 14 | #include "frc/Timer.h" |
| 15 | #include "frc/Utility.h" |
| 16 | #include "frc/WPIErrors.h" |
| 17 | |
| 18 | using namespace frc; |
| 19 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 20 | Notifier::Notifier(std::function<void()> handler) { |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 21 | if (handler == nullptr) |
| 22 | wpi_setWPIErrorWithContext(NullParameter, "handler must not be nullptr"); |
| 23 | m_handler = handler; |
| 24 | int32_t status = 0; |
| 25 | m_notifier = HAL_InitializeNotifier(&status); |
| 26 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 27 | |
| 28 | m_thread = std::thread([=] { |
| 29 | for (;;) { |
| 30 | int32_t status = 0; |
| 31 | HAL_NotifierHandle notifier = m_notifier.load(); |
| 32 | if (notifier == 0) break; |
| 33 | uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status); |
| 34 | if (curTime == 0 || status != 0) break; |
| 35 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 36 | std::function<void()> handler; |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 37 | { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 38 | std::scoped_lock lock(m_processMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 39 | handler = m_handler; |
| 40 | if (m_periodic) { |
| 41 | m_expirationTime += m_period; |
| 42 | UpdateAlarm(); |
| 43 | } else { |
| 44 | // need to update the alarm to cause it to wait again |
| 45 | UpdateAlarm(UINT64_MAX); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // call callback |
| 50 | if (handler) handler(); |
| 51 | } |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | Notifier::~Notifier() { |
| 56 | int32_t status = 0; |
| 57 | // atomically set handle to 0, then clean |
| 58 | HAL_NotifierHandle handle = m_notifier.exchange(0); |
| 59 | HAL_StopNotifier(handle, &status); |
| 60 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 61 | |
| 62 | // Join the thread to ensure the handler has exited. |
| 63 | if (m_thread.joinable()) m_thread.join(); |
| 64 | |
| 65 | HAL_CleanNotifier(handle, &status); |
| 66 | } |
| 67 | |
| 68 | Notifier::Notifier(Notifier&& rhs) |
| 69 | : ErrorBase(std::move(rhs)), |
| 70 | m_thread(std::move(rhs.m_thread)), |
| 71 | m_notifier(rhs.m_notifier.load()), |
| 72 | m_handler(std::move(rhs.m_handler)), |
| 73 | m_expirationTime(std::move(rhs.m_expirationTime)), |
| 74 | m_period(std::move(rhs.m_period)), |
| 75 | m_periodic(std::move(rhs.m_periodic)) { |
| 76 | rhs.m_notifier = HAL_kInvalidHandle; |
| 77 | } |
| 78 | |
| 79 | Notifier& Notifier::operator=(Notifier&& rhs) { |
| 80 | ErrorBase::operator=(std::move(rhs)); |
| 81 | |
| 82 | m_thread = std::move(rhs.m_thread); |
| 83 | m_notifier = rhs.m_notifier.load(); |
| 84 | rhs.m_notifier = HAL_kInvalidHandle; |
| 85 | m_handler = std::move(rhs.m_handler); |
| 86 | m_expirationTime = std::move(rhs.m_expirationTime); |
| 87 | m_period = std::move(rhs.m_period); |
| 88 | m_periodic = std::move(rhs.m_periodic); |
| 89 | |
| 90 | return *this; |
| 91 | } |
| 92 | |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 93 | void Notifier::SetHandler(std::function<void()> handler) { |
| 94 | std::scoped_lock lock(m_processMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 95 | m_handler = handler; |
| 96 | } |
| 97 | |
| 98 | void Notifier::StartSingle(double delay) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 99 | StartSingle(units::second_t(delay)); |
| 100 | } |
| 101 | |
| 102 | void Notifier::StartSingle(units::second_t delay) { |
| 103 | std::scoped_lock lock(m_processMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 104 | m_periodic = false; |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 105 | m_period = delay.to<double>(); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 106 | m_expirationTime = Timer::GetFPGATimestamp() + m_period; |
| 107 | UpdateAlarm(); |
| 108 | } |
| 109 | |
| 110 | void Notifier::StartPeriodic(double period) { |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 111 | StartPeriodic(units::second_t(period)); |
| 112 | } |
| 113 | |
| 114 | void Notifier::StartPeriodic(units::second_t period) { |
| 115 | std::scoped_lock lock(m_processMutex); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 116 | m_periodic = true; |
James Kuszmaul | 4f3ad3c | 2019-12-01 16:35:21 -0800 | [diff] [blame^] | 117 | m_period = period.to<double>(); |
Brian Silverman | 41cdd3e | 2019-01-19 19:48:58 -0800 | [diff] [blame] | 118 | m_expirationTime = Timer::GetFPGATimestamp() + m_period; |
| 119 | UpdateAlarm(); |
| 120 | } |
| 121 | |
| 122 | void Notifier::Stop() { |
| 123 | int32_t status = 0; |
| 124 | HAL_CancelNotifierAlarm(m_notifier, &status); |
| 125 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 126 | } |
| 127 | |
| 128 | void Notifier::UpdateAlarm(uint64_t triggerTime) { |
| 129 | int32_t status = 0; |
| 130 | // Return if we are being destructed, or were not created successfully |
| 131 | auto notifier = m_notifier.load(); |
| 132 | if (notifier == 0) return; |
| 133 | HAL_UpdateNotifierAlarm(notifier, triggerTime, &status); |
| 134 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 135 | } |
| 136 | |
| 137 | void Notifier::UpdateAlarm() { |
| 138 | UpdateAlarm(static_cast<uint64_t>(m_expirationTime * 1e6)); |
| 139 | } |