2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Notifier.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2019-11-08 22:53:20 -08:00
|
|
|
#include <hal/FRCUsageReporting.h>
|
|
|
|
|
#include <hal/Notifier.h>
|
2020-04-05 23:26:23 -07:00
|
|
|
#include <hal/Threads.h>
|
2019-11-09 11:41:58 -08:00
|
|
|
#include <wpi/SmallString.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2021-04-18 20:35:29 -07:00
|
|
|
#include "frc/Errors.h"
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Timer.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2019-07-20 22:57:16 -07:00
|
|
|
Notifier::Notifier(std::function<void()> handler) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!handler) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "handler");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
m_handler = handler;
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2017-11-19 17:58:40 -08:00
|
|
|
m_notifier = HAL_InitializeNotifier(&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "InitializeNotifier");
|
2017-11-19 17:58:40 -08:00
|
|
|
|
|
|
|
|
m_thread = std::thread([=] {
|
|
|
|
|
for (;;) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_NotifierHandle notifier = m_notifier.load();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (notifier == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-11-19 17:58:40 -08:00
|
|
|
uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (curTime == 0 || status != 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-11-19 17:58:40 -08:00
|
|
|
|
2019-07-20 22:57:16 -07:00
|
|
|
std::function<void()> handler;
|
2020-04-05 23:26:23 -07:00
|
|
|
{
|
|
|
|
|
std::scoped_lock lock(m_processMutex);
|
|
|
|
|
handler = m_handler;
|
|
|
|
|
if (m_periodic) {
|
|
|
|
|
m_expirationTime += m_period;
|
|
|
|
|
UpdateAlarm();
|
|
|
|
|
} else {
|
|
|
|
|
// need to update the alarm to cause it to wait again
|
|
|
|
|
UpdateAlarm(UINT64_MAX);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// call callback
|
2020-12-28 12:58:06 -08:00
|
|
|
if (handler) {
|
|
|
|
|
handler();
|
|
|
|
|
}
|
2020-04-05 23:26:23 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifier::Notifier(int priority, std::function<void()> handler) {
|
2021-04-18 20:35:29 -07:00
|
|
|
if (!handler) {
|
2021-05-23 19:33:33 -07:00
|
|
|
throw FRC_MakeError(err::NullParameter, "{}", "handler");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-04-05 23:26:23 -07:00
|
|
|
m_handler = handler;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
m_notifier = HAL_InitializeNotifier(&status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "InitializeNotifier");
|
2020-04-05 23:26:23 -07:00
|
|
|
|
|
|
|
|
m_thread = std::thread([=] {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetCurrentThreadPriority(true, priority, &status);
|
|
|
|
|
for (;;) {
|
|
|
|
|
HAL_NotifierHandle notifier = m_notifier.load();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (notifier == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-04-05 23:26:23 -07:00
|
|
|
uint64_t curTime = HAL_WaitForNotifierAlarm(notifier, &status);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (curTime == 0 || status != 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-04-05 23:26:23 -07:00
|
|
|
|
|
|
|
|
std::function<void()> handler;
|
2017-11-19 17:58:40 -08:00
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
2017-11-19 17:58:40 -08:00
|
|
|
handler = m_handler;
|
|
|
|
|
if (m_periodic) {
|
|
|
|
|
m_expirationTime += m_period;
|
|
|
|
|
UpdateAlarm();
|
2018-09-03 16:07:23 -07:00
|
|
|
} else {
|
|
|
|
|
// need to update the alarm to cause it to wait again
|
|
|
|
|
UpdateAlarm(UINT64_MAX);
|
2017-11-19 17:58:40 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// call callback
|
2020-12-28 12:58:06 -08:00
|
|
|
if (handler) {
|
|
|
|
|
handler();
|
|
|
|
|
}
|
2017-11-19 17:58:40 -08:00
|
|
|
}
|
|
|
|
|
});
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Notifier::~Notifier() {
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2016-06-05 07:29:47 -07:00
|
|
|
// atomically set handle to 0, then clean
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_NotifierHandle handle = m_notifier.exchange(0);
|
2017-11-19 17:58:40 -08:00
|
|
|
HAL_StopNotifier(handle, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_ReportError(status, "{}", "StopNotifier");
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-19 17:58:40 -08:00
|
|
|
// Join the thread to ensure the handler has exited.
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
m_thread.join();
|
|
|
|
|
}
|
2017-11-19 17:58:40 -08:00
|
|
|
|
|
|
|
|
HAL_CleanNotifier(handle, &status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
Notifier::Notifier(Notifier&& rhs)
|
2021-04-18 20:35:29 -07:00
|
|
|
: m_thread(std::move(rhs.m_thread)),
|
2018-09-24 00:08:25 -07:00
|
|
|
m_notifier(rhs.m_notifier.load()),
|
|
|
|
|
m_handler(std::move(rhs.m_handler)),
|
|
|
|
|
m_expirationTime(std::move(rhs.m_expirationTime)),
|
|
|
|
|
m_period(std::move(rhs.m_period)),
|
|
|
|
|
m_periodic(std::move(rhs.m_periodic)) {
|
|
|
|
|
rhs.m_notifier = HAL_kInvalidHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Notifier& Notifier::operator=(Notifier&& rhs) {
|
|
|
|
|
m_thread = std::move(rhs.m_thread);
|
|
|
|
|
m_notifier = rhs.m_notifier.load();
|
|
|
|
|
rhs.m_notifier = HAL_kInvalidHandle;
|
|
|
|
|
m_handler = std::move(rhs.m_handler);
|
|
|
|
|
m_expirationTime = std::move(rhs.m_expirationTime);
|
|
|
|
|
m_period = std::move(rhs.m_period);
|
|
|
|
|
m_periodic = std::move(rhs.m_periodic);
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-09 11:41:58 -08:00
|
|
|
void Notifier::SetName(const wpi::Twine& name) {
|
|
|
|
|
wpi::SmallString<64> nameBuf;
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetNotifierName(m_notifier,
|
|
|
|
|
name.toNullTerminatedStringRef(nameBuf).data(), &status);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 22:57:16 -07:00
|
|
|
void Notifier::SetHandler(std::function<void()> handler) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
2017-11-19 17:58:40 -08:00
|
|
|
m_handler = handler;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::StartSingle(double delay) {
|
2019-08-17 00:56:48 -04:00
|
|
|
StartSingle(units::second_t(delay));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::StartSingle(units::second_t delay) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_periodic = false;
|
2019-08-17 00:56:48 -04:00
|
|
|
m_period = delay.to<double>();
|
2017-11-27 21:59:00 -08:00
|
|
|
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
2015-12-29 10:41:31 -08:00
|
|
|
UpdateAlarm();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::StartPeriodic(double period) {
|
2019-08-17 00:56:48 -04:00
|
|
|
StartPeriodic(units::second_t(period));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Notifier::StartPeriodic(units::second_t period) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
2015-06-25 15:07:55 -04:00
|
|
|
m_periodic = true;
|
2019-08-17 00:56:48 -04:00
|
|
|
m_period = period.to<double>();
|
2017-11-27 21:59:00 -08:00
|
|
|
m_expirationTime = Timer::GetFPGATimestamp() + m_period;
|
2015-12-29 10:41:31 -08:00
|
|
|
UpdateAlarm();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::Stop() {
|
2020-02-18 20:38:05 -08:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
|
|
|
|
m_periodic = false;
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2017-11-19 17:58:40 -08:00
|
|
|
HAL_CancelNotifierAlarm(m_notifier, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "CancelNotifierAlarm");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2018-09-03 16:07:23 -07:00
|
|
|
void Notifier::UpdateAlarm(uint64_t triggerTime) {
|
2018-05-31 20:47:15 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
// Return if we are being destructed, or were not created successfully
|
|
|
|
|
auto notifier = m_notifier.load();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (notifier == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-09-03 16:07:23 -07:00
|
|
|
HAL_UpdateNotifierAlarm(notifier, triggerTime, &status);
|
2021-05-23 19:33:33 -07:00
|
|
|
FRC_CheckErrorStatus(status, "{}", "UpdateNotifierAlarm");
|
2018-05-31 20:47:15 -07:00
|
|
|
}
|
2018-09-03 16:07:23 -07:00
|
|
|
|
|
|
|
|
void Notifier::UpdateAlarm() {
|
|
|
|
|
UpdateAlarm(static_cast<uint64_t>(m_expirationTime * 1e6));
|
|
|
|
|
}
|
2021-02-28 22:05:26 -08:00
|
|
|
|
|
|
|
|
bool Notifier::SetHALThreadPriority(bool realTime, int32_t priority) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
return HAL_SetNotifierThreadPriority(realTime, priority, &status);
|
|
|
|
|
}
|