// $Id$
//
-// Copyright (C) 2006
-// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
-// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
-// Stefan Bund <stefan.bund@fokus.fraunhofer.de>
+// Copyright (C) 2006
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+// Stefan Bund <g0dil@berlios.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-// TODO: Implement signal handling
-// Here a basic concept of how to add signal support to the scheduler:
-//
-// Every signal to be reported by the scheduler will be asigned a
-// generic signal handler by the scheduler. This signal handler will
-// use longjmp (juck) to report this signal back to the scheduler
-// main-loop.
-//
-// To make this safe, the main-loop will look something like:
-//
-// int signal = setjmp(jmpBuffer_);
-// if (signal == 0) {
-// // unblock all signals which are registered with the
-// // scheduler
-// // call epoll
-// // block all relevant signals again
-// }
-//
-// // now handle the event
-//
-// The signal handler is then simply defined as
-//
-// static void Scheduler::sigHandler(int signal)
-// {
-// // make sure to restore the signal handler here if
-// // necessary
-// longjmp(Scheduler::instance().jmpBuffer_,signal);
-// }
-//
-// You should use sigaction to register the signal handlers and define
-// a sa_mask so all Scheduler-registered signals are automatically
-// *blocked* whenever one of the signals is called (including the
-// called signal!). This ensures, that no two signals can be delivered
-// on top of each other. And of course any signal registered with the
-// scheduler must be blocked as soon as it is registered with the
-// scheduler.
+/** \file
+ \brief Scheduler non-inline non-template implementation
-// TODO: Multithreading support
-// To support multithreading, the static member Scheduler::instance()
-// must return a thread-local value (that is Scheduler::instance()
-// must allocate one Scheduler instance per thread)
-
-// Definition of non-inline non-template functions
+ \idea Multithreading support: To support multithreading, the
+ static member Scheduler::instance() must return a thread-local
+ value (that is Scheduler::instance() must allocate one Scheduler
+ instance per thread). Another possibility would be to distribute
+ the async load unto several threads (one scheduler for multiple
+ threads)
+ */
#include "Scheduler.hh"
//#include "Scheduler.ih"
// Custom includes
-#include <errno.h>
-#include <sys/epoll.h>
-#include "Utils/Exception.hh"
-
-static const int EPollInitialSize = 16;
#define prefix_
///////////////////////////////cc.p////////////////////////////////////////
-prefix_ satcom::lib::Scheduler::Scheduler & satcom::lib::Scheduler::instance()
-{
- static Scheduler instance;
- return instance;
+namespace {
+ bool terminate_ (false);
}
-prefix_ satcom::lib::Scheduler::Scheduler()
- : epollFd_(epoll_create(EPollInitialSize))
+prefix_ void senf::scheduler::terminate()
{
- if (epollFd_<0)
- throw SystemException(errno);
+ terminate_ = true;
}
-prefix_ void satcom::lib::Scheduler::do_add(int fd, InternalCallback const & cb, EventId eventMask)
-{
- FdTable::iterator i (fdTable_.find(fd));
- int action (EPOLL_CTL_MOD);
- if (i == fdTable_.end()) {
- action = EPOLL_CTL_ADD;
- i = fdTable_.insert(std::make_pair(fd, EventSpec())).first;
- }
-
- if (eventMask & EV_READ) i->second.cb_read = cb;
- if (eventMask & EV_PRIO) i->second.cb_prio = cb;
- if (eventMask & EV_WRITE) i->second.cb_write = cb;
- if (eventMask & EV_HUP) i->second.cb_hup = cb;
- if (eventMask & EV_ERR) i->second.cb_err = cb;
-
- epoll_event ev;
- memset(&ev,0,sizeof(ev));
- ev.events = i->second.epollMask();
- ev.data.fd = fd;
+namespace {
- if (epoll_ctl(epollFd_, action, fd, &ev)<0)
- throw SystemException(errno);
+ // We don't want try { } catch(...) { ... throw; } since that will make debugging more
+ // difficult: the stack backtrace for an unexpected exception would always end here.
+ struct SchedulerScopedInit
+ {
+ SchedulerScopedInit()
+ {
+ senf::scheduler::detail::FIFORunner::instance().startWatchdog();
+ senf::scheduler::detail::SignalDispatcher::instance().unblockSignals();
+ senf::scheduler::detail::TimerDispatcher::instance().unblockSignals();
+ }
+
+ ~SchedulerScopedInit()
+ {
+ senf::scheduler::detail::TimerDispatcher::instance().blockSignals();
+ senf::scheduler::detail::SignalDispatcher::instance().blockSignals();
+ senf::scheduler::detail::FIFORunner::instance().stopWatchdog();
+ }
+ };
}
-prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask)
+prefix_ void senf::scheduler::process()
{
- FdTable::iterator i (fdTable_.find(fd));
- if (i == fdTable_.end())
- return;
-
- if (eventMask & EV_READ) i->second.cb_read = 0;
- if (eventMask & EV_PRIO) i->second.cb_prio = 0;
- if (eventMask & EV_WRITE) i->second.cb_write = 0;
- if (eventMask & EV_HUP) i->second.cb_hup = 0;
- if (eventMask & EV_ERR) i->second.cb_err = 0;
-
- epoll_event ev;
- memset(&ev,0,sizeof(ev));
- ev.events = i->second.epollMask();
- ev.data.fd = fd;
-
- int action (EPOLL_CTL_MOD);
- if (ev.events==0) {
- action = EPOLL_CTL_DEL;
- fdTable_.erase(i);
+ SchedulerScopedInit initScheduler;
+ terminate_ = false;
+ while(! terminate_ && ! (detail::FdDispatcher::instance().empty() &&
+ detail::TimerDispatcher::instance().empty() &&
+ detail::FileDispatcher::instance().empty())) {
+ detail::FdManager::instance().processOnce();
+ detail::FileDispatcher::instance().prepareRun();
+ detail::EventHookDispatcher::instance().prepareRun();
+ detail::FIFORunner::instance().run();
}
+}
- if (epoll_ctl(epollFd_, action, fd, &ev)<0)
- throw SystemException(errno);
+prefix_ void senf::scheduler::restart()
+{
+ detail::FdManager* fdm (&detail::FdManager::instance());
+ detail::FIFORunner* ffr (&detail::FIFORunner::instance());
+ detail::FdDispatcher* fdd (&detail::FdDispatcher::instance());
+ detail::TimerDispatcher* tdd (&detail::TimerDispatcher::instance());
+ detail::SignalDispatcher* sdd (&detail::SignalDispatcher::instance());
+ detail::FileDispatcher* fld (&detail::FileDispatcher::instance());
+ detail::EventHookDispatcher* eed (&detail::EventHookDispatcher::instance());
+
+ eed->~EventHookDispatcher();
+ fld->~FileDispatcher();
+ sdd->~SignalDispatcher();
+ tdd->~TimerDispatcher();
+ fdd->~FdDispatcher();
+ ffr->~FIFORunner();
+ fdm->~FdManager();
+
+ new (fdm) detail::FdManager();
+ new (ffr) detail::FIFORunner();
+ new (fdd) detail::FdDispatcher();
+ new (tdd) detail::TimerDispatcher();
+ new (sdd) detail::SignalDispatcher();
+ new (fld) detail::FileDispatcher();
+ new (eed) detail::EventHookDispatcher();
}
-prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
- const
+prefix_ bool senf::scheduler::empty()
{
- int mask (0);
- if (cb_read) mask |= EPOLLIN;
- if (cb_prio) mask |= EPOLLPRI;
- if (cb_write) mask |= EPOLLOUT;
- if (cb_hup) mask |= EPOLLHUP;
- if (cb_err) mask |= EPOLLERR;
- return mask;
+ return detail::FdDispatcher::instance().empty()
+ && detail::TimerDispatcher::instance().empty()
+ && detail::FileDispatcher::instance().empty()
+ && detail::SignalDispatcher::instance().empty()
+ && detail::EventHookDispatcher::instance().empty();
}
-prefix_ void satcom::lib::Scheduler::process()
+///////////////////////////////////////////////////////////////////////////
+// senf::schedulerLogTimeSource
+
+prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
+ const
{
- terminate_ = false;
- while (! terminate_) {
- struct epoll_event ev;
- int events = epoll_wait(epollFd_, &ev, 1, 1000);
- if (events<0)
- // Hmm ... man epoll says, it will NOT return with EINTR ??
- throw SystemException(errno);
- if (events==0)
- continue;
-
- FdTable::iterator i = fdTable_.find(ev.data.fd);
- BOOST_ASSERT (i != fdTable_.end() );
- EventSpec const & spec (i->second);
-
- if (ev.events & EPOLLIN) {
- BOOST_ASSERT(spec.cb_read);
- spec.cb_read(EV_READ);
- }
- else if (ev.events & EPOLLPRI) {
- BOOST_ASSERT(spec.cb_prio);
- spec.cb_prio(EV_PRIO);
- }
- else if (ev.events & EPOLLOUT) {
- BOOST_ASSERT(spec.cb_write);
- spec.cb_write(EV_WRITE);
- }
-
- else if (ev.events & EPOLLHUP) {
- BOOST_ASSERT(spec.cb_hup);
- spec.cb_hup(EV_HUP);
- }
- else if (ev.events & EPOLLERR) {
- BOOST_ASSERT(spec.cb_err);
- spec.cb_err(EV_ERR);
- }
- }
+ return eventTime();
}
///////////////////////////////cc.e////////////////////////////////////////
\f
// Local Variables:
// mode: c++
-// c-file-style: "satcom"
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
// End: