00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef _Timer_
00022 #define _Timer_
00023
00024 #include "qpid/sys/Monitor.h"
00025 #include "qpid/sys/Thread.h"
00026 #include "qpid/sys/Runnable.h"
00027 #include "qpid/RefCounted.h"
00028
00029 #include <memory>
00030 #include <queue>
00031
00032 #include <boost/intrusive_ptr.hpp>
00033
00034 namespace qpid {
00035 namespace broker {
00036
00037 struct TimerTask : public RefCounted {
00038 const qpid::sys::Duration duration;
00039 qpid::sys::AbsTime time;
00040 volatile bool cancelled;
00041
00042 TimerTask(qpid::sys::Duration timeout);
00043 TimerTask(qpid::sys::AbsTime time);
00044 virtual ~TimerTask();
00045 void reset();
00046 virtual void fire() = 0;
00047 };
00048
00049 struct Later {
00050 bool operator()(const boost::intrusive_ptr<TimerTask>& a,
00051 const boost::intrusive_ptr<TimerTask>& b) const;
00052 };
00053
00054 class Timer : private qpid::sys::Runnable {
00055 protected:
00056 qpid::sys::Monitor monitor;
00057 std::priority_queue<boost::intrusive_ptr<TimerTask>,
00058 std::vector<boost::intrusive_ptr<TimerTask> >,
00059 Later> tasks;
00060 qpid::sys::Thread runner;
00061 bool active;
00062
00063 virtual void run();
00064
00065 public:
00066 Timer();
00067 virtual ~Timer();
00068
00069 void add(boost::intrusive_ptr<TimerTask> task);
00070 void start();
00071 void stop();
00072
00073 };
00074
00075
00076 }}
00077
00078
00079 #endif