PPI: Clean up time interface
[senf.git] / Scheduler / Scheduler.hh
index 8fe4f15..3f6550c 100644 (file)
@@ -33,8 +33,8 @@
 #include <boost/function.hpp>
 #include <boost/utility.hpp>
 #include <boost/call_traits.hpp>
-
-#include "Utils/MicroTime.hh"
+#include <boost/integer.hpp>
+#include "ClockService.hh"
 
 //#include "scheduler.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
@@ -74,7 +74,7 @@ namespace senf {
         ///////////////////////////////////////////////////////////////////////////
         // Types
 
-        /// \brief Types of file descriptor events */
+        /** \brief Types of file descriptor events */
         enum EventId { EV_NONE=0,
                        EV_READ=1, EV_PRIO=2, EV_WRITE=4, 
                        EV_ALL=7,
@@ -92,6 +92,7 @@ namespace senf {
             typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
                                           EventId) > Callback;
         };
+
         /** \brief Callback type for timer events */
         typedef boost::function<void ()> TimerCallback;
 
@@ -146,13 +147,16 @@ namespace senf {
                                              \param[in] eventMask arbitrary combination via '|'
                                                  operator of EventId designators. */
 
-        void timeout(unsigned long timeout, TimerCallback const & cb); ///< Add timeout event
-                                        /**< \param[in] timeout timeout in milliseconds
+        unsigned timeout(ClockService::clock_type timeout, TimerCallback const & cb); 
+                                        ///< Add timeout event
+                                        /**< \param[in] timeout timeout in nanoseconds
                                              \param[in] cb callback to call after \a timeout
                                                  milliseconds
                                              \todo Return some kind of handle/pointer and add
                                                  support to update or revoke a timeout */
 
+        void cancelTimeout(unsigned id);
+
         void process();                 ///< Event handler main loop
                                         /**< This member must be called at some time to enter the
                                              event handler main loop. Only while this function is
@@ -163,12 +167,16 @@ namespace senf {
                                              main loop to terminate. The main loop will return to
                                              it's caller after the currently running callback
                                              returns. */
+        
+        ClockService::clock_type eventTime() const; ///< Return date/time of last event
 
     protected:
 
     private:
         typedef boost::function<void (EventId)> SimpleCallback;
 
+        static unsigned const MinTimeout = 1000;
+
         Scheduler();
 
         void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL);
@@ -190,23 +198,40 @@ namespace senf {
         struct TimerSpec
         {
             TimerSpec() : timeout(), cb() {}
-            TimerSpec(unsigned long long timeout_, TimerCallback cb_)
-                : timeout(timeout_), cb(cb_) {}
+            TimerSpec(ClockService::clock_type timeout_, TimerCallback cb_, unsigned id_)
+                : timeout(timeout_), cb(cb_), id(id_), canceled(false) {}
 
             bool operator< (TimerSpec const & other) const
                 { return timeout > other.timeout; }
 
-            unsigned long long timeout;
+            ClockService::clock_type timeout;
             TimerCallback cb;
+            unsigned id;
+            bool canceled;
         };
 
         typedef std::map<int,EventSpec> FdTable;
-        typedef std::priority_queue<TimerSpec> TimerQueue;
+        typedef std::map<unsigned,TimerSpec> TimerMap;
+
+        struct TimerSpecCompare
+        {
+            typedef TimerMap::iterator first_argument_type;
+            typedef TimerMap::iterator second_argument_type;
+            typedef bool result_type;
+            
+            result_type operator()(first_argument_type a, second_argument_type b);
+        };
+
+        typedef std::priority_queue<TimerMap::iterator, std::vector<TimerMap::iterator>, 
+                                    TimerSpecCompare> TimerQueue;
 
         FdTable fdTable_;
+        unsigned timerIdCounter_;
         TimerQueue timerQueue_;
+        TimerMap timerMap_;
         int epollFd_;
         bool terminate_;
+        ClockService::clock_type eventTime_;
     };
 
     /** \brief Default file descriptor accessor
@@ -233,4 +258,6 @@ namespace senf {
 // c-file-style: "senf"
 // indent-tabs-mode: nil
 // ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
 // End: