epollscheduler.h

00001 /*
00002  *  Copyright (C) 2006  Helmut Grohne
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #ifndef LIBMUTH_EPOLLSCHEDULER_H
00020 #define LIBMUTH_EPOLLSCHEDULER_H
00021 
00022 #ifndef USE_EPOLL
00023 #error this file must be compiled with -DUSE_EPOLL
00024 #endif
00025 
00026 #include <map>
00027 #include <sys/epoll.h>
00028 #include "basescheduler_decls.h"
00029 #include "callback_decls.h"
00030 #include "lock.h"
00031 #include "timeval_decls.h"
00032 #include "waitqueue.h"
00033 
00034 #define EPOLL_MAX_EVENTS 256
00035 
00039 class EpollScheduler : public BaseScheduler {
00040         private:
00041                 /*
00042                  * This lock locks all acceess to rfds, wfds and waits.
00043                  */
00044                 Lock datalock;
00045                 /*
00046                  * Whenever a Microthread wants to read or write from or to a
00047                  * filedescriptor it runs the scheduleReader or scheduleWriter
00048                  * method which will add the filedescriptor as key and the
00049                  * instance pointer as value to rfdsched or wfdsched. The pair
00050                  * will be discarded from the map when the filedescriptor
00051                  * becomes readable or writeable.
00052                  */
00053                 std::map<int, Callback*> rfds, wfds;
00054                 WaitQueue waits;
00055 
00056                 Lock notifylock;
00057                 bool notified;
00058                 int rpipe, wpipe;
00059                 void notifyme();
00063                 int efd;
00064                 void epoll_ctl(int op, int fd, struct epoll_event *event);
00065                 struct epoll_event events[EPOLL_MAX_EVENTS];
00066         protected:
00071                 EpollScheduler(BlockingQueue<Microthread*> *q=NULL);
00072         public:
00078                 static EpollScheduler &create(
00079                                 BlockingQueue<Microthread*> *q=NULL);
00080                 void addReadCallback(Callback &cb, int fd);
00081                 void addWriteCallback(Callback &cb, int fd);
00082                 void addWaitCallback(Callback &cb, const timeval &tv);
00083                 void run();
00084 };
00085 
00086 #endif

Generated on Sat Feb 7 01:26:50 2009 by  doxygen 1.5.1