Program Listing for File timer.h

Return to documentation for file (src/misc/pv/timer.h)

/* timer.h */
/*
 * Copyright information and license terms for this software can be
 * found in the file LICENSE that is included with the distribution
 */
#ifndef TIMER_H
#define TIMER_H
#include <memory>
#include <list>

#include <stddef.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>

#include <pv/pvType.h>
#include <pv/thread.h>
#include <pv/timeStamp.h>
#include <pv/event.h>
#include <pv/lock.h>
#include <pv/sharedPtr.h>

#include <shareLib.h>

namespace epics { namespace pvData {

class TimerCallback;
class Timer;
typedef std::tr1::shared_ptr<TimerCallback> TimerCallbackPtr;
typedef std::tr1::shared_ptr<Timer> TimerPtr;

class epicsShareClass TimerCallback {
public:
    POINTER_DEFINITIONS(TimerCallback);
    TimerCallback();
    virtual ~TimerCallback(){}
    virtual void callback() = 0;
    virtual void timerStopped() = 0;
private:
    epicsTime timeToRun;
    double period;
    bool onList;
    friend class Timer;
    struct IncreasingTime;
};

class epicsShareClass Timer : private Runnable {
public:
    POINTER_DEFINITIONS(Timer);
    Timer(std::string threadName, ThreadPriority priority);
    virtual ~Timer();
    void close();
    void scheduleAfterDelay(
        TimerCallbackPtr const &timerCallback,
        double delay);
    void schedulePeriodic(
        TimerCallbackPtr const &timerCallback,
        double delay,
        double period);
    bool cancel(TimerCallbackPtr const &timerCallback);
    bool isScheduled(TimerCallbackPtr const &timerCallback) const;
    void dump(std::ostream& o) const;

private:
    virtual void run();

    // call with mutex held
    void addElement(TimerCallbackPtr const &timerCallback);

    typedef std::list<TimerCallbackPtr> queue_t;

    mutable Mutex mutex;
    queue_t queue;
    Event waitForWork;
    bool waiting;
    bool alive;
    Thread thread;
};

epicsShareExtern std::ostream& operator<<(std::ostream& o, const Timer& timer);

}}
#endif  /* TIMER_H */