blob: a2f93f99c6e2df705580faca18cd111000975342 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -08002/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
Brian Silverman41cdd3e2019-01-19 19:48:58 -08003/* 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
18using namespace frc;
19
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080020Notifier::Notifier(std::function<void()> handler) {
Brian Silverman41cdd3e2019-01-19 19:48:58 -080021 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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080036 std::function<void()> handler;
Brian Silverman41cdd3e2019-01-19 19:48:58 -080037 {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080038 std::scoped_lock lock(m_processMutex);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080039 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
55Notifier::~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
68Notifier::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
79Notifier& 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 Kuszmaul4f3ad3c2019-12-01 16:35:21 -080093void Notifier::SetHandler(std::function<void()> handler) {
94 std::scoped_lock lock(m_processMutex);
Brian Silverman41cdd3e2019-01-19 19:48:58 -080095 m_handler = handler;
96}
97
98void Notifier::StartSingle(double delay) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -080099 StartSingle(units::second_t(delay));
100}
101
102void Notifier::StartSingle(units::second_t delay) {
103 std::scoped_lock lock(m_processMutex);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800104 m_periodic = false;
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800105 m_period = delay.to<double>();
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800106 m_expirationTime = Timer::GetFPGATimestamp() + m_period;
107 UpdateAlarm();
108}
109
110void Notifier::StartPeriodic(double period) {
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800111 StartPeriodic(units::second_t(period));
112}
113
114void Notifier::StartPeriodic(units::second_t period) {
115 std::scoped_lock lock(m_processMutex);
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800116 m_periodic = true;
James Kuszmaul4f3ad3c2019-12-01 16:35:21 -0800117 m_period = period.to<double>();
Brian Silverman41cdd3e2019-01-19 19:48:58 -0800118 m_expirationTime = Timer::GetFPGATimestamp() + m_period;
119 UpdateAlarm();
120}
121
122void Notifier::Stop() {
123 int32_t status = 0;
124 HAL_CancelNotifierAlarm(m_notifier, &status);
125 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
126}
127
128void 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
137void Notifier::UpdateAlarm() {
138 UpdateAlarm(static_cast<uint64_t>(m_expirationTime * 1e6));
139}