/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/broker/Timer.h

00001 /*
00002  *
00003  * Licensed to the Apache Software Foundation (ASF) under one
00004  * or more contributor license agreements.  See the NOTICE file
00005  * distributed with this work for additional information
00006  * regarding copyright ownership.  The ASF licenses this file
00007  * to you under the Apache License, Version 2.0 (the
00008  * "License"); you may not use this file except in compliance
00009  * with the License.  You may obtain a copy of the License at
00010  * 
00011  *   http://www.apache.org/licenses/LICENSE-2.0
00012  * 
00013  * Unless required by applicable law or agreed to in writing,
00014  * software distributed under the License is distributed on an
00015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00016  * KIND, either express or implied.  See the License for the
00017  * specific language governing permissions and limitations
00018  * under the License.
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

Generated on Thu Apr 10 11:08:17 2008 for Qpid by  doxygen 1.4.7