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
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/system/Notifier.hpp"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2018-09-24 00:08:25 -07:00
|
|
|
#include <utility>
|
|
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/DriverStation.h"
|
2026-01-04 00:41:53 -08:00
|
|
|
#include "wpi/hal/Notifier.hpp"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/hal/Threads.h"
|
|
|
|
|
#include "wpi/system/Errors.hpp"
|
2025-11-29 11:00:18 -08:00
|
|
|
#include "wpi/util/Synchronization.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
using namespace wpi;
|
2016-11-01 22:33:12 -07:00
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
Notifier::Notifier(std::function<void()> callback) {
|
|
|
|
|
if (!callback) {
|
2025-11-07 20:00:43 -05:00
|
|
|
throw WPILIB_MakeError(err::NullParameter, "callback");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2023-09-15 19:59:03 -07:00
|
|
|
m_callback = callback;
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2025-11-29 11:00:18 -08:00
|
|
|
m_notifier = HAL_CreateNotifier(&status);
|
|
|
|
|
WPILIB_CheckErrorStatus(status, "CreateNotifier");
|
2017-11-19 17:58:40 -08:00
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
m_thread = std::thread([=, this] {
|
2017-11-19 17:58:40 -08:00
|
|
|
for (;;) {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_NotifierHandle notifier = m_notifier.load();
|
2020-12-28 12:58:06 -08:00
|
|
|
if (notifier == 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-11-29 11:00:18 -08:00
|
|
|
if (WPI_WaitForObject(notifier) == 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2017-11-19 17:58:40 -08:00
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
std::function<void()> callback;
|
2020-04-05 23:26:23 -07:00
|
|
|
{
|
|
|
|
|
std::scoped_lock lock(m_processMutex);
|
2023-09-15 19:59:03 -07:00
|
|
|
callback = m_callback;
|
2020-04-05 23:26:23 -07:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
// Call callback
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2025-11-29 11:00:18 -08:00
|
|
|
|
|
|
|
|
// Ack notifier
|
2025-12-09 20:28:15 -07:00
|
|
|
HAL_AcknowledgeNotifierAlarm(notifier, &status);
|
2025-11-29 11:00:18 -08:00
|
|
|
WPILIB_CheckErrorStatus(status, "AcknowledgeNotifier");
|
2020-04-05 23:26:23 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
Notifier::Notifier(int priority, std::function<void()> callback) {
|
|
|
|
|
if (!callback) {
|
2025-11-07 20:00:43 -05:00
|
|
|
throw WPILIB_MakeError(err::NullParameter, "callback");
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2023-09-15 19:59:03 -07:00
|
|
|
m_callback = callback;
|
2020-04-05 23:26:23 -07:00
|
|
|
int32_t status = 0;
|
2025-11-29 11:00:18 -08:00
|
|
|
m_notifier = HAL_CreateNotifier(&status);
|
2025-11-07 20:00:43 -05:00
|
|
|
WPILIB_CheckErrorStatus(status, "InitializeNotifier");
|
2020-04-05 23:26:23 -07:00
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
m_thread = std::thread([=, this] {
|
2020-04-05 23:26:23 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2025-11-29 11:00:18 -08:00
|
|
|
if (WPI_WaitForObject(notifier) == 0) {
|
2020-12-28 12:58:06 -08:00
|
|
|
break;
|
|
|
|
|
}
|
2020-04-05 23:26:23 -07:00
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
std::function<void()> callback;
|
2017-11-19 17:58:40 -08:00
|
|
|
{
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
2023-09-15 19:59:03 -07:00
|
|
|
callback = m_callback;
|
2017-11-19 17:58:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// call callback
|
2023-09-15 19:59:03 -07:00
|
|
|
if (callback) {
|
2022-12-12 01:42:22 -05:00
|
|
|
try {
|
2023-09-15 19:59:03 -07:00
|
|
|
callback();
|
2025-11-07 20:00:05 -05:00
|
|
|
} catch (const wpi::RuntimeError& e) {
|
2022-12-12 01:42:22 -05:00
|
|
|
e.Report();
|
2025-11-07 20:00:43 -05:00
|
|
|
WPILIB_ReportError(
|
2022-12-12 01:42:22 -05:00
|
|
|
err::Error,
|
|
|
|
|
"Error in Notifier thread."
|
|
|
|
|
" The above stacktrace can help determine where the error "
|
|
|
|
|
"occurred.\n"
|
|
|
|
|
" See https://wpilib.org/stacktrace for more information.\n");
|
|
|
|
|
throw;
|
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
|
HAL_SendError(1, err::Error, 0, e.what(), "", "", 1);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2025-11-29 11:00:18 -08:00
|
|
|
|
|
|
|
|
// Ack notifier
|
2025-12-09 20:28:15 -07:00
|
|
|
HAL_AcknowledgeNotifierAlarm(notifier, &status);
|
2025-11-29 11:00:18 -08:00
|
|
|
WPILIB_CheckErrorStatus(status, "AcknowledgeNotifier");
|
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() {
|
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);
|
2025-11-29 11:00:18 -08:00
|
|
|
HAL_DestroyNotifier(handle);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
// Join the thread to ensure the callback has exited.
|
2020-12-28 12:58:06 -08:00
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
m_thread.join();
|
|
|
|
|
}
|
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()),
|
2025-11-29 11:00:18 -08:00
|
|
|
m_callback(std::move(rhs.m_callback)) {
|
2018-09-24 00:08:25 -07:00
|
|
|
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;
|
2023-09-15 19:59:03 -07:00
|
|
|
m_callback = std::move(rhs.m_callback);
|
2018-09-24 00:08:25 -07:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 17:44:18 -07:00
|
|
|
void Notifier::SetName(std::string_view name) {
|
2019-11-09 11:41:58 -08:00
|
|
|
int32_t status = 0;
|
2025-11-29 11:00:18 -08:00
|
|
|
HAL_SetNotifierName(m_notifier, name, &status);
|
2019-11-09 11:41:58 -08:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 19:59:03 -07:00
|
|
|
void Notifier::SetCallback(std::function<void()> callback) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_processMutex);
|
2023-09-15 19:59:03 -07:00
|
|
|
m_callback = callback;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
void Notifier::StartSingle(wpi::units::second_t delay) {
|
2025-11-29 11:00:18 -08:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetNotifierAlarm(m_notifier, static_cast<uint64_t>(delay * 1e6), 0, false,
|
2025-12-09 20:28:15 -07:00
|
|
|
false, &status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
void Notifier::StartPeriodic(wpi::units::second_t period) {
|
2025-11-29 11:00:18 -08:00
|
|
|
int32_t status = 0;
|
|
|
|
|
HAL_SetNotifierAlarm(m_notifier, static_cast<uint64_t>(period * 1e6),
|
2025-12-09 20:28:15 -07:00
|
|
|
static_cast<uint64_t>(period * 1e6), false, false,
|
|
|
|
|
&status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
void Notifier::StartPeriodic(wpi::units::hertz_t frequency) {
|
2025-10-29 03:18:55 +00:00
|
|
|
StartPeriodic(1 / frequency);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void Notifier::Stop() {
|
2015-12-29 10:41:31 -08:00
|
|
|
int32_t status = 0;
|
2025-12-09 20:28:15 -07:00
|
|
|
HAL_CancelNotifierAlarm(m_notifier, false, &status);
|
2025-11-07 20:00:43 -05:00
|
|
|
WPILIB_CheckErrorStatus(status, "CancelNotifierAlarm");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2025-11-29 11:00:18 -08:00
|
|
|
int32_t Notifier::GetOverrun() const {
|
2018-05-31 20:47:15 -07:00
|
|
|
int32_t status = 0;
|
2025-11-29 11:00:18 -08:00
|
|
|
int32_t overrun = HAL_GetNotifierOverrun(m_notifier, &status);
|
|
|
|
|
WPILIB_CheckErrorStatus(status, "GetNotifierOverrun");
|
|
|
|
|
return overrun;
|
2018-09-03 16:07:23 -07:00
|
|
|
}
|
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);
|
|
|
|
|
}
|