mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Adds a static mutex to notifiers to stop destructor race condition (#204)
This commit is contained in:
committed by
Peter Johnson
parent
776cb915bc
commit
40365faeda
@@ -11,6 +11,8 @@
|
||||
#include "Utility.h"
|
||||
#include "WPIErrors.h"
|
||||
|
||||
priority_mutex Notifier::m_destructorMutex;
|
||||
|
||||
/**
|
||||
* Create a Notifier for timer event notification.
|
||||
*
|
||||
@@ -39,7 +41,8 @@ Notifier::~Notifier() {
|
||||
/* Acquire the mutex; this makes certain that the handler is not being
|
||||
* executed by the interrupt manager.
|
||||
*/
|
||||
std::lock_guard<priority_mutex> lock(m_notifyMutex);
|
||||
std::lock_guard<priority_mutex> lockStatic(Notifier::m_destructorMutex);
|
||||
std::lock_guard<priority_mutex> lock(m_processMutex);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,6 +50,8 @@ Notifier::~Notifier() {
|
||||
*/
|
||||
void Notifier::UpdateAlarm() {
|
||||
int32_t status = 0;
|
||||
// Return if we are being destructed, or were not created successfully
|
||||
if (m_notifier == 0) return;
|
||||
HAL_UpdateNotifierAlarm(
|
||||
m_notifier, static_cast<uint64_t>(m_expirationTime * 1e6), &status);
|
||||
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
||||
@@ -56,10 +61,18 @@ void Notifier::UpdateAlarm() {
|
||||
* Notify is called by the HAL layer. We simply need to pass it through to
|
||||
* the user handler.
|
||||
*/
|
||||
void Notifier::Notify(uint64_t currentTimeInt, void* param) {
|
||||
Notifier* notifier = static_cast<Notifier*>(param);
|
||||
void Notifier::Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle) {
|
||||
Notifier* notifier;
|
||||
{
|
||||
// Lock static mutex to grab the notifier param
|
||||
std::lock_guard<priority_mutex> lock(Notifier::m_destructorMutex);
|
||||
int32_t status = 0;
|
||||
auto notifierPointer = HAL_GetNotifierParam(handle, &status);
|
||||
if (notifierPointer == nullptr) return;
|
||||
notifier = static_cast<Notifier*>(notifierPointer);
|
||||
notifier->m_processMutex.lock();
|
||||
}
|
||||
|
||||
notifier->m_processMutex.lock();
|
||||
if (notifier->m_periodic) {
|
||||
notifier->m_expirationTime += notifier->m_period;
|
||||
notifier->UpdateAlarm();
|
||||
@@ -67,11 +80,8 @@ void Notifier::Notify(uint64_t currentTimeInt, void* param) {
|
||||
|
||||
auto handler = notifier->m_handler;
|
||||
|
||||
notifier->m_notifyMutex.lock();
|
||||
notifier->m_processMutex.unlock();
|
||||
|
||||
if (handler) handler();
|
||||
notifier->m_notifyMutex.unlock();
|
||||
notifier->m_processMutex.unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,5 +133,6 @@ void Notifier::Stop() {
|
||||
|
||||
// Wait for a currently executing handler to complete before returning from
|
||||
// Stop()
|
||||
std::lock_guard<priority_mutex> sync(m_notifyMutex);
|
||||
std::lock_guard<priority_mutex> lockStatic(Notifier::m_destructorMutex);
|
||||
std::lock_guard<priority_mutex> lock(m_processMutex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user