X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Scheduler%2FScheduler.hh;h=babc1997e4883d56c9c89d6fee7359649b84a9c8;hb=82ad2ed94c12c3e53097fef92978de8c28239fab;hp=a8dc456aed0469edb4052d98339fe717cf86c3d2;hpb=553781d9e9bce316dca24ac4f0c42e5613e849e0;p=senf.git diff --git a/Scheduler/Scheduler.hh b/Scheduler/Scheduler.hh index a8dc456..babc199 100644 --- a/Scheduler/Scheduler.hh +++ b/Scheduler/Scheduler.hh @@ -1,6 +1,6 @@ // $Id$ // -// Copyright (C) 2006 +// Copyright (C) 2006 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) // Kompetenzzentrum fuer Satelitenkommunikation (SatCom) // Stefan Bund @@ -33,8 +33,9 @@ #include #include #include - -#include "Utils/MicroTime.hh" +#include +#include "ClockService.hh" +#include "../Utils/Logger/Target.hh" //#include "scheduler.mpp" ///////////////////////////////hh.p//////////////////////////////////////// @@ -45,27 +46,27 @@ namespace senf { /** \brief Singleton class to manage the event loop This class manages a single select() type event loop. A customer of this class may register - any number of file descriptiors with this class and pass callback functions to be called on + any number of file descriptors with this class and pass callback functions to be called on input, output or error. This functions are specified using boost::function objects (See Boost.Function) - The Scheduler is based on a generic handle representation. The only information needed from - a handle, is the intrinsic file descriptor. Any object for which the statement - \code - int fd = retrieve_filehandle(object); - \endcode - is valid and places the relevent file descriptor into fd can be used as a Handle type. There - is an implementation of retrieve_filehandle(int) within the library to handle explicit file - descrptors. The Socket library provides an - implementation of retrive_filehandle(FileHandle handle). If you want to support - some other handle type, just define an apropriate \c retrieve_filehandle function in - that types namespace. - - It is important to note, that for every combination of file descriptor and event, only a \e - single handler may be installed. Installing more handlers does not make sense. If you need - to distribute data to serveral interested parties, you must take care of this yourself. - - \todo Fix EventId parameter (probably to int) to allow |-ing without casting ... + The Scheduler is based on a generic handle representation. The only information needed from + a handle, is the intrinsic file descriptor. Any object for which the statement + \code + int fd = retrieve_filehandle(object); + \endcode + is valid and places the relevant file descriptor into fd can be used as a Handle type. There + is an implementation of retrieve_filehandle(int) within the library to handle explicit file + descriptors. The Socket library provides an + implementation of retrieve_filehandle(FileHandle handle). If you want to support + some other handle type, just define an appropriate \c retrieve_filehandle function in + that types namespace. + + It is important to note, that for every combination of file descriptor and event, only a \e + single handler may be installed. Installing more handlers does not make sense. If you need + to distribute data to several interested parties, you must take care of this yourself. + + \todo Fix EventId parameter (probably to int) to allow |-ing without casting ... */ class Scheduler : boost::noncopyable @@ -74,25 +75,27 @@ namespace senf { /////////////////////////////////////////////////////////////////////////// // Types - /// \brief Types of file descriptor events */ - enum EventId { EV_NONE=0, - EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16, - EV_ALL=31 }; + /** \brief Types of file descriptor events */ + enum EventId { EV_NONE=0, + EV_READ=1, EV_PRIO=2, EV_WRITE=4, + EV_ALL=7, + EV_HUP=8, EV_ERR=16 }; + + /** \brief Template typedef for Callback type - /** \brief Template typedef for Callback type - - This is a template typedef (which does not exist in C++) that is, a template class whose - sole member is a typedef symbol defining the callback type given the handle type. + This is a template typedef (which does not exist in C++) that is, a template class whose + sole member is a typedef symbol defining the callback type given the handle type. - The Callback is any callable object taking a \c Handle and an \c EventId as argument. - */ + The Callback is any callable object taking a \c Handle and an \c EventId as argument. + */ template struct GenericCallback { typedef boost::function::param_type, EventId) > Callback; }; - /** \brief Callback type for timer events */ - typedef boost::function TimerCallback; + + /** \brief Callback type for timer events */ + typedef boost::function TimerCallback; /////////////////////////////////////////////////////////////////////////// ///\name Structors and default members @@ -104,121 +107,150 @@ namespace senf { // default destructor // no conversion constructors - /** \brief Return Scheduler instance - - This static member is used to access the singleton instance. This member is save to - return a correctly initialized Scheduler instance even if called at global construction - time - - \implementation This static member just defines the Scheduler as a static method - variable. The C++ standard then provides above guaratee. The instance will be - initialized the first time, the code flow passes the variable declaration found in - the instance() body. - */ + /** \brief Return Scheduler instance + + This static member is used to access the singleton instance. This member is save to + return a correctly initialized Scheduler instance even if called at global construction + time + + \implementation This static member just defines the Scheduler as a static method + variable. The C++ standard then provides above guarantee. The instance will be + initialized the first time, the code flow passes the variable declaration found in + the instance() body. + */ static Scheduler & instance(); ///@} /////////////////////////////////////////////////////////////////////////// template - void add(Handle const & handle, + void add(Handle const & handle, typename GenericCallback::Callback const & cb, int eventMask = EV_ALL); ///< Add file handle event callback /**< add() will add a callback to the Scheduler. The - callbeck will be called for the given type of event on - the given arbitrary file-descriptor or - handle-like object. If there already is a Callback - register ed for one of the events requested, the new - handler will replace the old one. - \param[in] handle file descriptor or handle providing - the Handle interface defined above. - \param[in] cb callback - \param[in] eventMask arbitrary combination via '|' - operator of EventId designators. */ - template + callback will be called for the given type of event on + the given arbitrary file-descriptor or + handle-like object. If there already is a Callback + registered for one of the events requested, the new + handler will replace the old one. + \param[in] handle file descriptor or handle providing + the Handle interface defined above. + \param[in] cb callback + \param[in] eventMask arbitrary combination via '|' + operator of EventId designators. */ + template void remove(Handle const & handle, int eventMask = EV_ALL); ///< Remove event callback /**< remove() will remove any callback registered for any of - the given events on the given file descriptor or handle - like object. - \param[in] handle file descriptor or handle providing - the Handle interface defined above. - \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 - \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 */ + the given events on the given file descriptor or handle + like object. + \param[in] handle file descriptor or handle providing + the Handle interface defined above. + \param[in] eventMask arbitrary combination via '|' + operator of EventId designators. */ + + 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 */ + + 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 - running any events are handled. The call will return - only, if any callback calls terminate(). */ + event handler main loop. Only while this function is + running any events are handled. The call will return + only, if any callback calls terminate(). */ void terminate(); ///< Called by callbacks to terminate the main loop /**< This member may be called by any callback to tell the - main loop to terminate. The main loop will return to - it's caller after the currently running callback - returns. */ + 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 SimpleCallback; + typedef boost::function SimpleCallback; Scheduler(); - + void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL); void do_remove(int fd, int eventMask = EV_ALL); - /** \brief Descriptor event specification - \internal */ - struct EventSpec + /** \brief Descriptor event specification + \internal */ + struct EventSpec { SimpleCallback cb_read; SimpleCallback cb_prio; SimpleCallback cb_write; - SimpleCallback cb_hup; - SimpleCallback cb_err; int epollMask() const; }; - - /** \brief Timer event specification - \internal */ - struct TimerSpec - { - TimerSpec() : timeout(), cb() {} - TimerSpec(unsigned long long timeout_, TimerCallback cb_) - : timeout(timeout_), cb(cb_) {} - - bool operator< (TimerSpec const & other) const - { return timeout > other.timeout; } - - unsigned long long timeout; - TimerCallback cb; - }; - + + /** \brief Timer event specification + \internal */ + struct TimerSpec + { + TimerSpec() : timeout(), 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; } + + ClockService::clock_type timeout; + TimerCallback cb; + unsigned id; + bool canceled; + }; + typedef std::map FdTable; - typedef std::priority_queue TimerQueue; + typedef std::map TimerMap; // sorted by id + + 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, + TimerSpecCompare> TimerQueue; // sorted by time FdTable fdTable_; - TimerQueue timerQueue_; + unsigned timerIdCounter_; + TimerQueue timerQueue_; + TimerMap timerMap_; int epollFd_; bool terminate_; + ClockService::clock_type eventTime_; }; /** \brief Default file descriptor accessor - - retrieve_filehandle() provides the Scheduler with support for explicit file descriptors as - file handle argument. - \relates Scheduler + retrieve_filehandle() provides the Scheduler with support for explicit file descriptors as + file handle argument. + + \relates Scheduler */ int retrieve_filehandle(int fd); + /** \brief Scheduler specific time source for Utils/Logger framework + + This time source may be used to provide timing information for log messages within the + Utils/Logger framework. This time source will use Scheduler::eventTime() to provide timing + information. + */ + struct SchedulerLogTimeSource : public senf::log::TimeSource + { + boost::posix_time::ptime operator()() const; + }; + } ///////////////////////////////hh.e//////////////////////////////////////// @@ -230,6 +262,10 @@ namespace senf { // Local Variables: // mode: c++ -// c-file-style: "senf" // fill-column: 100 +// c-file-style: "senf" +// indent-tabs-mode: nil +// ispell-local-dictionary: "american" +// compile-command: "scons -u test" +// comment-column: 40 // End: