4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.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.
24 \brief Scheduler public header
28 #define HH_Scheduler_ 1
31 #include "../Utils/Logger/SenfLog.hh"
33 #include "TimerEvent.hh"
34 #include "SignalEvent.hh"
36 //#include "scheduler.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
41 /** \brief Visible scheduler interface
43 The %scheduler singleton manages access to the %scheduler library. It provides access to
44 several event dispatchers:
45 \li File descriptor notifications
49 The %scheduler is entered by calling it's process() member. This call will continue to run as
50 long as there is something to do, or until one of the handlers calls terminate(). The
51 %scheduler has 'something to do' as long as there is any file descriptor or timeout active.
53 The %scheduler only provides low level primitive scheduling capability. Additional helpers
54 are defined on top of this functionality (e.g. ReadHelper or WriteHelper or the interval
58 \section sched_handlers Specifying handlers
60 All handlers are passed as generic <a
61 href="http://www.boost.org/doc/html/function.html">Boost.Function</a> objects. This allows
62 to pass any callable as a handler. Depending on the type of handler, some additional
63 arguments may be passed to the handler by the %scheduler.
65 If you need to pass additional information to your handler, use <a
66 href="http://www.boost.org/libs/bind/bind.html">Boost.Bind</a>:
68 // Handle callback function
69 void callback(UDPv4ClientSocketHandle handle, senf::Scheduler::EventId event) {..}
70 // Pass 'handle' as additional first argument to callback()
71 Scheduler::instance().add(handle, boost::bind(&callback, handle, _1), EV_READ)
73 void timeout( int n) {..}
74 // Call timeout() handler with argument 'n'
75 Scheduler::instance().timeout(boost::bind(&timeout, n))
78 To use member-functions as callbacks, use either <a
79 href="http://www.boost.org/libs/bind/bind.html">Boost.Bind</a> or senf::membind()
81 // e.g. in Foo::Foo() constructor:
82 Scheduler::instance().add(handle_, senf::membind(&Foo::callback, this)), EV_READ)
85 The handler can also be identified by an arbitrary, user specified name. This name is used
86 in error messages to identify the failing handler.
89 \section sched_fd Registering file descriptors
91 File descriptors are managed using add() or remove()
93 Scheduler::instance().add(handle, &callback, EV_ALL);
94 Scheduler::instance().remove(handle);
97 The callback will be called with one additional argument. This argument is the event mask of
98 type EventId. This mask will tell, which of the registered events are signaled. The
99 additional flags EV_HUP or EV_ERR (on hangup or error condition) may be set additionally.
101 Only a single handler may be registered for any combination of file descriptor and event
102 (registering multiple callbacks for a single fd and event does not make sense).
104 The %scheduler will accept any object as \a handle argument as long as retrieve_filehandle()
105 may be called on that object
107 int fd = retrieve_filehandle(handle);
109 to fetch the file handle given some abstract handle type. retrieve_filehandle() will be
110 found using ADL depending on the argument namespace. A default implementation is provided
111 for \c int arguments (file descriptors)
114 \section sched_timers Registering timers
116 The %scheduler has very simple timer support. There is only one type of timer: A single-shot
117 deadline timer. More complex timers are built based on this. Timers are managed using
118 timeout() and cancelTimeout()
120 int id = Scheduler::instance().timeout(Scheduler::instance().eventTime() + ClockService::milliseconds(100),
122 Scheduler::instance().cancelTimeout(id);
124 Timing is based on the ClockService, which provides a high resolution and strictly
125 monotonous time source which again is based on POSIX timers. Registering a timeout will fire
126 the callback when the target time is reached. The timer may be canceled by passing the
127 returned \a id to cancelTimeout().
130 \section sched_signals Registering POSIX/UNIX signals
132 The %scheduler also incorporates standard POSIX/UNIX signals. Signals registered with the
133 %scheduler will be handled \e synchronously within the event loop.
135 Scheduler::instance().registerSignal(SIGUSR1, &callback);
136 Scheduler::instance().unregisterSignal(SIGUSR1);
138 When registering a signal with the %scheduler, that signal will automatically be blocked so
139 it can be handled within the %scheduler.
141 A registered signal does \e not count as 'something to do'. It is therefore not possible to
142 wait for signals \e only.
144 \todo Change the Scheduler API to use RAII. Additionally, this will remove all dynamic
145 memory allocations from the scheduler.
146 \todo Fix the file support to use threads (?) fork (?) and a pipe so it works reliably even
149 namespace scheduler {
151 /** \brief Event handler main loop
153 This member must be called at some time to enter the event handler main loop. Only while
154 this function is running any events are handled. The call will return if
155 \li a callback calls terminate()
156 \li the run queue becomes empty.
160 /** \brief Called by callbacks to terminate the main loop
162 This member may be called by any callback to tell the main loop to terminate. The main loop
163 will return to it's caller after the currently running callback returns.
167 /** \brief Return date/time of last event
169 This is the timestamp, the last event has been signaled. This is the real time at which the
170 event is delivered \e not the time it should have been delivered (in the case of timers).
172 ClockService::clock_type eventTime();
174 /** \brief Set task watchdog timeout */
175 void taskTimeout(unsigned ms);
177 /** \brief Current task watchdog timeout */
178 unsigned taskTimeout();
180 /** \brief Number of watchdog events */
181 unsigned hangCount();
183 /** \brief Restart scheduler
185 This call will restart all scheduler dispatchers (timers, signals, file descriptors). This
186 is necessary after a fork().
187 \warning This call will \e remove all registered events from the scheduler
191 /** \brief %scheduler specific time source for Utils/Logger framework
193 This time source may be used to provide timing information for log messages within the
194 Utils/Logger framework. This time source will use Scheduler::eventTime() to provide timing
197 Using this information reduces the number of necessary ClockService::now() calls and thus
198 the number of system calls.
200 struct LogTimeSource : public senf::log::TimeSource
202 senf::log::time_type operator()() const;
207 ///////////////////////////////hh.e////////////////////////////////////////
208 #include "Scheduler.cci"
209 //#include "Scheduler.ct"
210 //#include "Scheduler.cti"
217 // c-file-style: "senf"
218 // indent-tabs-mode: nil
219 // ispell-local-dictionary: "american"
220 // compile-command: "scons -u test"
221 // comment-column: 40