selectscheduler.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_SELECTSCHEDULER_H
00020 #define LIBMUTH_SELECTSCHEDULER_H
00021 
00022 #include <map>
00023 #include <sys/types.h>
00024 #ifndef __WIN32__
00025 #include <sys/time.h>
00026 #include <unistd.h>
00027 #include <sys/select.h>
00028 #else
00029 #include "win32.h"
00030 #endif
00031 #include "basescheduler_decls.h"
00032 #include "callback_decls.h"
00033 #include "lock.h"
00034 #include "timeval_decls.h"
00035 #include "waitqueue.h"
00036 
00040 class SelectScheduler : public BaseScheduler {
00041         private:
00042                 /*
00043                  * This lock locks all accesses to rfds, wfds and waits.
00044                  */
00045                 Lock datalock;
00046                 /*
00047                  * Whenever a Microthread wants to read or write from or to a
00048                  * filedescriptor it runs the scheduleReader or scheduleWriter
00049                  * method which will add the filedescriptor as key and the
00050                  * instance pointer as value to rfdsched or wfdsched. The pair
00051                  * will be discarded from the map when the filedescriptor
00052                  * becomes readable or writable.
00053                  */
00054                 std::map<int, Callback*> rfds, wfds, efds;
00055                 WaitQueue waits;
00056 
00057                 Lock notifylock;
00058                 bool notified;
00059                 int rpipe, wpipe;
00060                 void notifyme();
00061                 fd_set rfdset[1], wfdset[1], efdset[1];
00062         protected:
00067                 SelectScheduler(BlockingQueue<Microthread*> *q=NULL);
00068         public:
00074                 static SelectScheduler &create(
00075                                 BlockingQueue<Microthread*> *q=NULL);
00076                 void addReadCallback(Callback &cb, int fd);
00077                 void addWriteCallback(Callback &cb, int fd);
00084                 void addExceptCallback(Callback &cb, int fd);
00085                 void addWaitCallback(Callback &cb, const timeval &tv);
00086                 void run();
00087 };
00088 
00089 #endif

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