Skip to content
Snippets Groups Projects
Scheduler.cc 7.53 KiB
Newer Older
sbund's avatar
sbund committed
// $Id$
//
// Copyright (C) 2006
sbund's avatar
sbund committed
// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
//     Stefan Bund <stefan.bund@fokus.fraunhofer.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
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

    \brief Scheduler non-inline non-template implementation

    \idea Implement signal handling (See source for more discussion
    about this)

    \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)
// 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 also means, we will have to re-register all
// signals if we change the registration of some signal since the
// sa_mask changes). 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.
sbund's avatar
sbund committed
#include "Scheduler.hh"
//#include "Scheduler.ih"

// Custom includes
#include <errno.h>
#include <sys/epoll.h>
sbund's avatar
sbund committed
#include "Utils/Exception.hh"
g0dil's avatar
g0dil committed
#include "Utils/MicroTime.hh"
sbund's avatar
sbund committed

static const int EPollInitialSize = 16;

#define prefix_
///////////////////////////////cc.p////////////////////////////////////////

prefix_ senf::Scheduler::Scheduler & senf::Scheduler::instance()
sbund's avatar
sbund committed
{
    static Scheduler instance;
    return instance;
}

prefix_ void senf::Scheduler::timeout(unsigned long timeout, TimerCallback const & cb)
g0dil's avatar
g0dil committed
{
    timerQueue_.push(TimerSpec(now()+1000*timeout,cb));
}

prefix_ senf::Scheduler::Scheduler()
sbund's avatar
sbund committed
    : epollFd_(epoll_create(EPollInitialSize))
{
    if (epollFd_<0)
        throw SystemException(errno);
}

prefix_ void senf::Scheduler::do_add(int fd, SimpleCallback const & cb, int eventMask)
sbund's avatar
sbund committed
{
    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;
    }

sbund's avatar
sbund committed
    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;
sbund's avatar
sbund committed

    epoll_event ev;
    memset(&ev,0,sizeof(ev));
    ev.events = i->second.epollMask();
sbund's avatar
sbund committed
    ev.data.fd = fd;
sbund's avatar
sbund committed
    if (epoll_ctl(epollFd_, action, fd, &ev)<0)
        throw SystemException(errno);
}

prefix_ void senf::Scheduler::do_remove(int fd, int eventMask)
sbund's avatar
sbund committed
{
    FdTable::iterator i (fdTable_.find(fd));
    if (i == fdTable_.end())
sbund's avatar
sbund committed
        return;

sbund's avatar
sbund committed
    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;
sbund's avatar
sbund committed

    epoll_event ev;
    memset(&ev,0,sizeof(ev));
    ev.events = i->second.epollMask();
sbund's avatar
sbund committed
    ev.data.fd = fd;
sbund's avatar
sbund committed
    int action (EPOLL_CTL_MOD);
    if (ev.events==0) {
        action = EPOLL_CTL_DEL;
        fdTable_.erase(i);
    }

    if (epoll_ctl(epollFd_, action, fd, &ev)<0)
        throw SystemException(errno);
}

prefix_ int senf::Scheduler::EventSpec::epollMask()
sbund's avatar
sbund committed
    const
{
    int mask (0);
sbund's avatar
sbund committed
    if (cb_read)  mask |= EPOLLIN;
    if (cb_prio)  mask |= EPOLLPRI;
sbund's avatar
sbund committed
    if (cb_write) mask |= EPOLLOUT;
    return mask;
}

prefix_ void senf::Scheduler::process()
sbund's avatar
sbund committed
{
    terminate_ = false;
    while (! terminate_) {
g0dil's avatar
g0dil committed

        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);

g0dil's avatar
g0dil committed
        struct epoll_event ev;
g0dil's avatar
g0dil committed
        int events = epoll_wait(epollFd_, &ev, 1, timeout);
sbund's avatar
sbund committed
        if (events<0)
g0dil's avatar
g0dil committed
            // Hmm ... man epoll says, it will NOT return with EINTR. I hope, this is true :-)
sbund's avatar
sbund committed
            throw SystemException(errno);
        if (events==0)
g0dil's avatar
g0dil committed
            // Timeout .. the handler will be run when going back to the loop top
sbund's avatar
sbund committed
            continue;
sbund's avatar
sbund committed
        FdTable::iterator i = fdTable_.find(ev.data.fd);
        BOOST_ASSERT (i != fdTable_.end() );
g0dil's avatar
g0dil committed
        // \todo Make this more efficient. Instead of copying the event-spec it should be
        // revalidated by monitoring add/remove calls
        EventSpec spec (i->second);

        unsigned extraFlags (0);
        if (ev.events & EPOLLHUP) extraFlags |= EV_HUP;
        if (ev.events & EPOLLERR) extraFlags |= EV_ERR;
sbund's avatar
sbund committed

        if (ev.events & EPOLLIN) {
            BOOST_ASSERT(spec.cb_read);
g0dil's avatar
g0dil committed
            spec.cb_read(EventId(EV_READ | extraFlags));
sbund's avatar
sbund committed
        }
        else if (ev.events & EPOLLPRI) {
            BOOST_ASSERT(spec.cb_prio);
g0dil's avatar
g0dil committed
            spec.cb_prio(EventId(EV_PRIO | extraFlags));
sbund's avatar
sbund committed
        }
        else if (ev.events & EPOLLOUT) {
            BOOST_ASSERT(spec.cb_write);
g0dil's avatar
g0dil committed
            spec.cb_write(EventId(EV_WRITE | extraFlags));
sbund's avatar
sbund committed
        }
g0dil's avatar
g0dil committed
        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));
sbund's avatar
sbund committed
        }
    }
}

sbund's avatar
sbund committed
///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_


// Local Variables:
// mode: c++
// fill-column: 100
// c-file-style: "senf"
// indent-tabs-mode: nil
// ispell-local-dictionary: "american"
// compile-command: "scons -u test"
// comment-column: 40
sbund's avatar
sbund committed
// End: