blob: 69a9f4ed1933c2b7a97579ecc5afacdef8075906 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
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
18using namespace frc;
19
20Notifier::Notifier(TimerEventHandler handler) {
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
36 TimerEventHandler handler;
37 {
38 std::lock_guard<wpi::mutex> lock(m_processMutex);
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
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
93void Notifier::SetHandler(TimerEventHandler handler) {
94 std::lock_guard<wpi::mutex> lock(m_processMutex);
95 m_handler = handler;
96}
97
98void Notifier::StartSingle(double delay) {
99 std::lock_guard<wpi::mutex> lock(m_processMutex);
100 m_periodic = false;
101 m_period = delay;
102 m_expirationTime = Timer::GetFPGATimestamp() + m_period;
103 UpdateAlarm();
104}
105
106void Notifier::StartPeriodic(double period) {
107 std::lock_guard<wpi::mutex> lock(m_processMutex);
108 m_periodic = true;
109 m_period = period;
110 m_expirationTime = Timer::GetFPGATimestamp() + m_period;
111 UpdateAlarm();
112}
113
114void Notifier::Stop() {
115 int32_t status = 0;
116 HAL_CancelNotifierAlarm(m_notifier, &status);
117 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
118}
119
120void Notifier::UpdateAlarm(uint64_t triggerTime) {
121 int32_t status = 0;
122 // Return if we are being destructed, or were not created successfully
123 auto notifier = m_notifier.load();
124 if (notifier == 0) return;
125 HAL_UpdateNotifierAlarm(notifier, triggerTime, &status);
126 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
127}
128
129void Notifier::UpdateAlarm() {
130 UpdateAlarm(static_cast<uint64_t>(m_expirationTime * 1e6));
131}