2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
#include "ErrorBase.h"
|
2015-06-25 01:54:20 -07:00
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
2015-07-02 14:33:24 -04:00
|
|
|
#include <thread>
|
|
|
|
|
#include <atomic>
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-10-07 17:13:31 -04:00
|
|
|
typedef void (*TimerEventHandler)(void *param);
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
class Notifier : public ErrorBase {
|
|
|
|
|
public:
|
2015-06-23 04:49:51 -07:00
|
|
|
Notifier(TimerEventHandler handler, void *param = nullptr);
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual ~Notifier();
|
2015-07-21 01:23:34 -07:00
|
|
|
|
|
|
|
|
Notifier(const Notifier&) = delete;
|
|
|
|
|
Notifier& operator=(const Notifier&) = delete;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void StartSingle(double delay);
|
|
|
|
|
void StartPeriodic(double period);
|
|
|
|
|
void Stop();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
|
|
|
|
static Notifier *timerQueueHead;
|
2015-06-25 01:54:20 -07:00
|
|
|
static priority_recursive_mutex queueMutex;
|
2015-06-25 15:07:55 -04:00
|
|
|
static void *m_notifier;
|
|
|
|
|
static int refcount;
|
2014-05-02 17:54:01 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
static void ProcessQueue(
|
|
|
|
|
uint32_t mask, void *params); // process the timer queue on a timer event
|
|
|
|
|
static void
|
|
|
|
|
UpdateAlarm(); // update the FPGA alarm since the queue has changed
|
|
|
|
|
void InsertInQueue(
|
|
|
|
|
bool reschedule); // insert this Notifier in the timer queue
|
|
|
|
|
void DeleteFromQueue(); // delete this Notifier from the timer queue
|
|
|
|
|
TimerEventHandler m_handler; // address of the handler
|
|
|
|
|
void *m_param; // a parameter to pass to the handler
|
2015-06-24 01:06:29 -07:00
|
|
|
double m_period = 0; // the relative time (either periodic or single)
|
|
|
|
|
double m_expirationTime = 0; // absolute expiration time for the current event
|
|
|
|
|
Notifier *m_nextEvent = nullptr; // next Nofifier event
|
|
|
|
|
bool m_periodic = false; // true if this is a periodic event
|
|
|
|
|
bool m_queued = false; // indicates if this entry is queued
|
2015-06-25 01:54:20 -07:00
|
|
|
priority_mutex m_handlerMutex; // held by interrupt manager task while
|
2015-06-25 15:07:55 -04:00
|
|
|
// handler call is in progress
|
2014-08-08 17:05:49 -04:00
|
|
|
|
2015-07-02 14:33:24 -04:00
|
|
|
#ifdef FRC_SIMULATOR
|
2015-07-29 16:48:04 -04:00
|
|
|
static std::thread m_task;
|
|
|
|
|
static std::atomic<bool> m_stopped;
|
2015-07-02 14:33:24 -04:00
|
|
|
#endif
|
2015-06-25 15:07:55 -04:00
|
|
|
static void Run();
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|