4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // TODO: Fix EventId parameter (probably to int) to allow |-ing without casting ...
26 /** \mainpage The SENF Scheduler library
31 #define HH_Scheduler_ 1
36 #include <boost/function.hpp>
37 #include <boost/utility.hpp>
38 #include <boost/call_traits.hpp>
40 #include "Utils/MicroTime.hh"
42 //#include "scheduler.mpp"
43 ///////////////////////////////hh.p////////////////////////////////////////
45 /** \brief SENF Project namespace */
48 /** \brief Singleton class to manage the event loop
50 This class manages a single select() type event loop. A
51 customer of this class may register any number of file
52 descriptiors with this class and pass callback functions to be
53 called on input, output or error. This functions are specified
54 using boost::function objects
60 ///////////////////////////////////////////////////////////////////////////
63 enum EventId { EV_NONE=0,
64 EV_READ=1, EV_PRIO=2, EV_WRITE=4, EV_HUP=8, EV_ERR=16,
67 template <class Handle>
68 struct GenericCallback {
69 typedef boost::function<void (typename boost::call_traits<Handle>::param_type,
72 typedef boost::function<void (EventId)> SimpleCallback;
73 typedef boost::function<void ()> TimerCallback;
75 ///////////////////////////////////////////////////////////////////////////
76 ///\name Structors and default members
79 // private default constructor
80 // no copy constructor
83 // no conversion constructors
85 static Scheduler & instance();
88 ///////////////////////////////////////////////////////////////////////////
90 template <class Handle>
91 void add(Handle const & handle,
92 typename GenericCallback<Handle>::Callback const & cb,
93 int eventMask = EV_ALL);
94 template <class Handle>
95 void remove(Handle const & handle, int eventMask = EV_ALL);
97 void timeout(unsigned long timeout, TimerCallback const & cb);
107 void do_add(int fd, SimpleCallback const & cb, int eventMask = EV_ALL);
108 void do_remove(int fd, int eventMask = EV_ALL);
112 SimpleCallback cb_read;
113 SimpleCallback cb_prio;
114 SimpleCallback cb_write;
115 SimpleCallback cb_hup;
116 SimpleCallback cb_err;
118 int epollMask() const;
123 TimerSpec() : timeout(), cb() {}
124 TimerSpec(unsigned long long timeout_, TimerCallback cb_)
125 : timeout(timeout_), cb(cb_) {}
127 bool operator< (TimerSpec const & other) const
128 { return timeout > other.timeout; }
130 unsigned long long timeout;
134 typedef std::map<int,EventSpec> FdTable;
135 typedef std::priority_queue<TimerSpec> TimerQueue;
138 TimerQueue timerQueue_;
143 int retrieve_filehandle(int fd);
147 ///////////////////////////////hh.e////////////////////////////////////////
148 #include "Scheduler.cci"
149 #include "Scheduler.ct"
150 #include "Scheduler.cti"
156 // c-file-style: "senf"