removed some useless spaces; not very important, I know :)
[senf.git] / Scheduler / Scheduler.cc
index 4f39c4b..b19e988 100644 (file)
@@ -1,9 +1,9 @@
 // $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)
+    \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)
+ */
 
-// Definition of non-inline non-template functions
+// Here a basic concept of how to add signal support to the scheduler:
+//
+// ... no, I had overlooked one race condition. So back to the signal-pipe approach ...
 
 #include "Scheduler.hh"
 //#include "Scheduler.ih"
 
 // Custom includes
+#include "../Utils/senfassert.hh"
 #include <errno.h>
 #include <sys/epoll.h>
-#include "Utils/Exception.hh"
-#include "Utils/MicroTime.hh"
+#include <unistd.h>
+#include <fcntl.h>
+#include "../Utils/Exception.hh"
 
 static const int EPollInitialSize = 16;
 
 #define prefix_
 ///////////////////////////////cc.p////////////////////////////////////////
 
-prefix_ satcom::lib::Scheduler::Scheduler & satcom::lib::Scheduler::instance()
+prefix_ senf::Scheduler::Scheduler()
+    : files_(0), timerIdCounter_(0), epollFd_ (epoll_create(EPollInitialSize)), terminate_(false),
+      eventTime_(0), eventEarly_(ClockService::milliseconds(11)), eventAdjust_(0)
 {
-    static Scheduler instance;
-    return instance;
+    if (epollFd_<0)
+        throwErrno();
+
+    if (::pipe(sigpipe_) < 0)
+        throwErrno();
+
+    int flags (::fcntl(sigpipe_[1],F_GETFL));
+    if (flags < 0) 
+        throwErrno();
+    flags |= O_NONBLOCK;
+    if (::fcntl(sigpipe_[1], F_SETFL, flags) < 0) 
+        throwErrno();
+
+    ::epoll_event ev;
+    ::memset(&ev, 0, sizeof(ev));
+    ev.events = EV_READ;
+    ev.data.fd = sigpipe_[0];
+    if (::epoll_ctl(epollFd_, EPOLL_CTL_ADD, sigpipe_[0], &ev) < 0)
+        throwErrno();
 }
 
-prefix_ void satcom::lib::Scheduler::timeout(unsigned long timeout, TimerCallback const & cb)
+prefix_ void senf::Scheduler::registerSignal(unsigned signal, SimpleCallback const & cb)
 {
-    timerQueue_.push(TimerSpec(now()+1000*timeout,cb));
+    ::sigset_t sig;
+    ::sigemptyset(&sig);
+    if (::sigaddset(&sig, signal) < 0)
+        throw InvalidSignalNumberException();
+    ::sigprocmask(SIG_BLOCK, &sig, 0);
+    ::sigaddset(&sigset_, signal);
+    if (sigHandlers_.size() <= signal)
+        sigHandlers_.resize(signal+1);
+    sigHandlers_[signal] = cb;
+
+    registerSigHandlers();
 }
 
-prefix_ satcom::lib::Scheduler::Scheduler()
-    : epollFd_(epoll_create(EPollInitialSize))
+prefix_ void senf::Scheduler::unregisterSignal(unsigned signal)
 {
-    if (epollFd_<0)
-        throw SystemException(errno);
+    if (::sigdelset(&sigset_, signal) < 0)
+        throw InvalidSignalNumberException();
+    sigHandlers_[signal] = 0;
+    ::signal(signal, SIG_DFL);
+    registerSigHandlers();
 }
 
-prefix_ void satcom::lib::Scheduler::do_add(int fd, SimpleCallback const & cb, EventId eventMask)
+prefix_ void senf::Scheduler::do_add(int fd, FdCallback const & cb, int eventMask)
 {
+    if (eventMask == 0)
+        return;
+
     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 (i->second.epollMask() == 0) {
+        action = EPOLL_CTL_ADD;
+        fdErase_.erase( std::remove(fdErase_.begin(), fdErase_.end(), unsigned(fd)),
+                        fdErase_.end() );
+    }
 
     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;
-    
-    if (epoll_ctl(epollFd_, action, fd, &ev)<0)
-        throw SystemException(errno);
+
+    if (! i->second.file && epoll_ctl(epollFd_, action, fd, &ev) < 0) {
+        if (errno == EPERM) {
+            // Argh ... epoll does not support ordinary files :-( :-(
+            i->second.file = true;
+            ++ files_;
+        }
+        else
+            throwErrno("::epoll_ctl()");
+    }
 }
 
-prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask)
+prefix_ void senf::Scheduler::do_remove(int fd, int eventMask)
 {
+    if (eventMask == 0)
+        return;
+
     FdTable::iterator i (fdTable_.find(fd));
-    if (i == fdTable_.end()) 
+    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));
@@ -138,83 +156,195 @@ prefix_ void satcom::lib::Scheduler::do_remove(int fd, EventId eventMask)
     ev.data.fd = fd;
 
     int action (EPOLL_CTL_MOD);
+    bool file (i->second.file);
     if (ev.events==0) {
         action = EPOLL_CTL_DEL;
-        fdTable_.erase(i);
+        fdErase_.push_back(fd);
     }
 
-    if (epoll_ctl(epollFd_, action, fd, &ev)<0)
-        throw SystemException(errno);
+    if (! file && epoll_ctl(epollFd_, action, fd, &ev) < 0)
+        throwErrno("::epoll_ctl()");
+    if (file)
+        -- files_;
+}
+
+prefix_ void senf::Scheduler::registerSigHandlers()
+{
+    for (unsigned signal (1); signal < sigHandlers_.size(); ++signal) {
+        if (sigHandlers_[signal]) {
+            struct ::sigaction sa;
+            sa.sa_sigaction = & Scheduler::sigHandler;
+            sa.sa_mask = sigset_;
+            sa.sa_flags = SA_SIGINFO;
+            if (signal == SIGCHLD)
+                sa.sa_flags |= SA_NOCLDSTOP;
+            if (::sigaction(signal, &sa, 0) < 0)
+                throwErrno();
+        }
+    }
 }
 
+prefix_ void senf::Scheduler::sigHandler(int signal, ::siginfo_t * siginfo, void *)
+{
+    // This is a bit unsafe. Better write single bytes and place the siginfo into an explicit
+    // queue. Since signals are only unblocked during epoll_wait, we even wouldn't need to
+    // synchronize access to that queue any further.
+
+    ::write(instance().sigpipe_[1], siginfo, sizeof(*siginfo));
+
+    // We ignore errors. The file handle is set to non-blocking IO. If any failure occurs (pipe
+    // full), the signal will be dropped. That's like kernel signal handling which may also drop
+    // signals.
+}
 
-prefix_ int satcom::lib::Scheduler::EventSpec::epollMask()
+prefix_ int senf::Scheduler::EventSpec::epollMask()
     const
 {
     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;
 }
 
-prefix_ void satcom::lib::Scheduler::process()
+prefix_ void senf::Scheduler::process()
 {
     terminate_ = false;
+    eventTime_ = ClockService::now();
     while (! terminate_) {
-        struct epoll_event ev;
-       MicroTime timeNow = now();
-       while ( ! timerQueue_.empty() && timerQueue_.top().timeout <= timeNow ) {
-           timerQueue_.top().cb();
-           timerQueue_.pop();
-       }
-       if (terminate_) 
-           return;
-       int timeout = timerQueue_.empty() ? -1 : int((timerQueue_.top().timeout - timeNow)/1000);
-           
-        int events = epoll_wait(epollFd_, &ev, 1, timeout);
-        if (events<0)
-            // Hmm ... man epoll says, it will NOT return with EINTR ??
-            throw SystemException(errno);
-        if (events==0)
-           // Timeout .. it will be run when reachiung the top of the loop
-            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);
+        // Since a callback may have disabled further timers, we need to check for canceled timeouts
+        // again.
+
+        while (! timerQueue_.empty()) {
+            TimerMap::iterator i (timerQueue_.top());
+            if (! i->second.canceled)
+                break;
+            timerMap_.erase(i);
+            timerQueue_.pop();
         }
-        else if (ev.events & EPOLLPRI) {
-            BOOST_ASSERT(spec.cb_prio);
-            spec.cb_prio(EV_PRIO);
+
+        for (FdEraseList::iterator i (fdErase_.begin()); i != fdErase_.end(); ++i) 
+            fdTable_.erase(*i);
+        fdErase_.clear();
+
+        int timeout (-1);
+        if (files_ > 0)
+            timeout = 0;
+        else {
+            if (timerQueue_.empty()) {
+                if (fdTable_.empty())
+                    break;
+            }
+            else {
+                ClockService::clock_type delta (
+                    (timerQueue_.top()->second.timeout - eventTime_ + eventAdjust_)/1000000UL);
+                timeout = delta < 0 ? 0 : delta;
+            }
         }
-        else if (ev.events & EPOLLOUT) {
-            BOOST_ASSERT(spec.cb_write);
-            spec.cb_write(EV_WRITE);
+
+        ///\todo Handle more than one epoll_event per call
+        struct epoll_event ev;
+        
+        ::sigprocmask(SIG_UNBLOCK, &sigset_, 0);
+        int events (epoll_wait(epollFd_, &ev, 1, timeout));
+        ::sigprocmask(SIG_BLOCK, &sigset_, 0);
+
+        if (events<0)
+            if (errno != EINTR)
+                throwErrno();
+
+        eventTime_ = ClockService::now();
+
+        // We always run timeout handlers. This is important, even if a file-descriptor is signaled
+        // since some descriptors (e.g. real files) will *always* be ready and we still may want to
+        // handle timers.  Time handlers are run before file events to not delay them unnecessarily.
+
+        while (! timerQueue_.empty()) {
+            TimerMap::iterator i (timerQueue_.top());
+            if (i->second.canceled)
+                ;
+            else if (i->second.timeout <= eventTime_ + eventEarly_)
+                i->second.cb();
+            else
+                break;
+            timerQueue_.pop();
+            timerMap_.erase(i);
         }
 
-        else if (ev.events & EPOLLHUP) {
-            BOOST_ASSERT(spec.cb_hup);
-            spec.cb_hup(EV_HUP);
+        // Check the signal queue
+        if (events > 0 && ev.data.fd == sigpipe_[0]) {
+            ::siginfo_t siginfo;
+            if (::read(sigpipe_[0], &siginfo, sizeof(siginfo)) < int(sizeof(siginfo))) {
+                // We ignore truncated records which may only occur if the signal
+                // queue became filled up
+                SENF_LOG((senf::log::IMPORTANT)("Truncated signal record!"));
+                continue;
+            }
+            if (siginfo.si_signo < int(sigHandlers_.size()) && sigHandlers_[siginfo.si_signo])
+                sigHandlers_[siginfo.si_signo]();
+            continue;
         }
-        else if (ev.events & EPOLLERR) {
-            BOOST_ASSERT(spec.cb_err);
-            spec.cb_err(EV_ERR);
+
+        for (FdTable::iterator i = fdTable_.begin(); i != fdTable_.end(); ++i) {
+            EventSpec & spec (i->second);
+
+            if (! (spec.file || (events > 0 && i->first == ev.data.fd)))
+                continue;
+                
+            unsigned extraFlags (0);
+            unsigned mask (spec.file ? spec.epollMask() : ev.events);
+
+            if (mask & EPOLLHUP) extraFlags |= EV_HUP;
+            if (mask & EPOLLERR) extraFlags |= EV_ERR;
+
+            if (mask & EPOLLIN) {
+                SENF_ASSERT(spec.cb_read);
+                spec.cb_read(EventId(EV_READ | extraFlags));
+            }
+            else if (mask & EPOLLPRI) {
+                SENF_ASSERT(spec.cb_prio);
+                spec.cb_prio(EventId(EV_PRIO | extraFlags));
+            }
+            else if (mask & EPOLLOUT) {
+                SENF_ASSERT(spec.cb_write);
+                spec.cb_write(EventId(EV_WRITE | extraFlags));
+            }
+            else {
+                // This branch is only taken, if HUP or ERR is signaled but none of IN/OUT/PRI. 
+                // In this case we will signal all registered callbacks. The callbacks must be
+                // prepared to be called multiple times if they are registered to more than
+                // one event.
+                if (spec.cb_write) 
+                    spec.cb_write(EventId(extraFlags));
+                if (spec.cb_prio) 
+                    spec.cb_prio(EventId(extraFlags));
+                if (spec.cb_read) 
+                    spec.cb_read(EventId(extraFlags));
+            }
         }
     }
 }
 
+///////////////////////////////////////////////////////////////////////////
+// senf::SchedulerLogTimeSource
+
+prefix_ boost::posix_time::ptime senf::SchedulerLogTimeSource::operator()()
+    const
+{
+    return ClockService::abstime(Scheduler::instance().eventTime());
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 
 \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: