2014-05-02 17:54:01 -04:00
|
|
|
#include "HAL/Notifier.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "ChipObject.h"
|
2015-11-21 22:17:11 -08:00
|
|
|
#include "HAL/HAL.hpp"
|
|
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <mutex>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
static const uint32_t kTimerInterruptNumber = 28;
|
|
|
|
|
|
2015-11-21 22:17:11 -08:00
|
|
|
static priority_mutex notifierInterruptMutex;
|
|
|
|
|
static priority_recursive_mutex notifierMutex;
|
|
|
|
|
static tAlarm *notifierAlarm = nullptr;
|
|
|
|
|
static tInterruptManager *notifierManager = nullptr;
|
|
|
|
|
static uint32_t closestTrigger = UINT32_MAX;
|
|
|
|
|
struct Notifier {
|
|
|
|
|
Notifier *prev, *next;
|
|
|
|
|
void *param;
|
|
|
|
|
void (*process)(uint32_t, void*);
|
|
|
|
|
uint32_t triggerTime = UINT32_MAX;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2015-11-21 22:17:11 -08:00
|
|
|
static Notifier *notifiers = nullptr;
|
|
|
|
|
static std::atomic_flag notifierAtexitRegistered = ATOMIC_FLAG_INIT;
|
|
|
|
|
static std::atomic_int notifierRefCount = ATOMIC_VAR_INIT(0);
|
|
|
|
|
|
|
|
|
|
static void alarmCallback(uint32_t, void*)
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<priority_recursive_mutex> sync(notifierMutex);
|
|
|
|
|
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
uint32_t currentTime = 0;
|
|
|
|
|
|
|
|
|
|
// the hardware disables itself after each alarm
|
|
|
|
|
closestTrigger = UINT32_MAX;
|
|
|
|
|
|
|
|
|
|
// process all notifiers
|
|
|
|
|
Notifier *notifier = notifiers;
|
|
|
|
|
while (notifier) {
|
|
|
|
|
if (notifier->triggerTime != UINT32_MAX) {
|
|
|
|
|
if (currentTime == 0)
|
|
|
|
|
currentTime = getFPGATime(&status);
|
|
|
|
|
if (notifier->triggerTime < currentTime) {
|
|
|
|
|
notifier->triggerTime = UINT32_MAX;
|
|
|
|
|
auto process = notifier->process;
|
|
|
|
|
auto param = notifier->param;
|
|
|
|
|
sync.unlock();
|
|
|
|
|
process(currentTime, param);
|
|
|
|
|
sync.lock();
|
|
|
|
|
} else if (notifier->triggerTime < closestTrigger) {
|
|
|
|
|
updateNotifierAlarm(notifier, notifier->triggerTime, &status);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
notifier = notifier->next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void cleanupNotifierAtExit() {
|
|
|
|
|
notifierAlarm = nullptr;
|
|
|
|
|
notifierManager = nullptr;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-11-21 22:17:11 -08:00
|
|
|
void* initializeNotifier(void (*ProcessQueue)(uint32_t, void*), void *param, int32_t *status)
|
2014-05-02 17:54:01 -04:00
|
|
|
{
|
2015-11-21 22:17:11 -08:00
|
|
|
if (!ProcessQueue) {
|
|
|
|
|
*status = NULL_PARAMETER;
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (!notifierAtexitRegistered.test_and_set())
|
|
|
|
|
std::atexit(cleanupNotifierAtExit);
|
|
|
|
|
if (notifierRefCount.fetch_add(1) == 0) {
|
|
|
|
|
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
|
|
|
|
|
// create manager and alarm if not already created
|
|
|
|
|
if (!notifierManager) {
|
|
|
|
|
notifierManager = new tInterruptManager(1 << kTimerInterruptNumber, false, status);
|
|
|
|
|
notifierManager->registerHandler(alarmCallback, NULL, status);
|
|
|
|
|
notifierManager->enable(status);
|
|
|
|
|
}
|
|
|
|
|
if (!notifierAlarm) notifierAlarm = tAlarm::create(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
|
|
|
|
// create notifier structure and add to list
|
2014-05-02 17:54:01 -04:00
|
|
|
Notifier* notifier = new Notifier();
|
2015-11-21 22:17:11 -08:00
|
|
|
notifier->prev = nullptr;
|
|
|
|
|
notifier->next = notifiers;
|
|
|
|
|
if (notifier->next) notifier->next->prev = notifier;
|
|
|
|
|
notifier->param = param;
|
|
|
|
|
notifier->process = ProcessQueue;
|
|
|
|
|
notifiers = notifier;
|
2014-05-02 17:54:01 -04:00
|
|
|
return notifier;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
void cleanNotifier(void* notifier_pointer, int32_t *status)
|
|
|
|
|
{
|
2015-11-21 22:17:11 -08:00
|
|
|
{
|
|
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
|
|
|
|
Notifier* notifier = (Notifier*)notifier_pointer;
|
|
|
|
|
|
|
|
|
|
// remove from list and delete
|
|
|
|
|
if (notifier->prev) notifier->prev->next = notifier->next;
|
|
|
|
|
if (notifier->next) notifier->next->prev = notifier->prev;
|
|
|
|
|
if (notifiers == notifier) notifiers = notifier->next;
|
|
|
|
|
delete notifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (notifierRefCount.fetch_sub(1) == 1) {
|
|
|
|
|
std::lock_guard<priority_mutex> sync(notifierInterruptMutex);
|
|
|
|
|
// if this was the last notifier, clean up alarm and manager
|
|
|
|
|
if (notifierAlarm) {
|
|
|
|
|
notifierAlarm->writeEnable(false, status);
|
|
|
|
|
delete notifierAlarm;
|
|
|
|
|
notifierAlarm = nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (notifierManager) {
|
|
|
|
|
notifierManager->disable(status);
|
|
|
|
|
delete notifierManager;
|
|
|
|
|
notifierManager = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
void updateNotifierAlarm(void* notifier_pointer, uint32_t triggerTime, int32_t *status)
|
|
|
|
|
{
|
2015-11-21 22:17:11 -08:00
|
|
|
std::lock_guard<priority_recursive_mutex> sync(notifierMutex);
|
|
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
Notifier* notifier = (Notifier*)notifier_pointer;
|
2015-11-21 22:17:11 -08:00
|
|
|
notifier->triggerTime = triggerTime;
|
|
|
|
|
bool wasActive = (closestTrigger != UINT32_MAX);
|
|
|
|
|
|
|
|
|
|
if (!notifierInterruptMutex.try_lock() || notifierRefCount == 0 ||
|
|
|
|
|
!notifierAlarm)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Update alarm time if closer than current.
|
|
|
|
|
if (triggerTime < closestTrigger) {
|
|
|
|
|
closestTrigger = triggerTime;
|
|
|
|
|
notifierAlarm->writeTriggerTime(triggerTime, status);
|
|
|
|
|
}
|
2014-05-02 17:54:01 -04:00
|
|
|
// Enable the alarm. The hardware disables itself after each alarm.
|
2015-11-21 22:17:11 -08:00
|
|
|
if (!wasActive) notifierAlarm->writeEnable(true, status);
|
|
|
|
|
|
|
|
|
|
notifierInterruptMutex.unlock();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|