channel_decls.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_CHANNEL_DECLS_H
00020 #define LIBMUTH_CHANNEL_DECLS_H
00021 
00022 #include <list>
00023 #include <set>
00024 #include <utility>
00025 #include <queue>
00026 #ifndef __WIN32__
00027 #include <stdint.h>
00028 #else
00029 #include "win32.h"
00030 #endif
00031 #include "lock.h"
00032 
00033 typedef uint64_t MessageID;
00034 
00035 class BaseChannel;
00036 class Microthread;
00037 
00043 class ChannelManager {
00044         private:
00045                 Lock datalock;
00046                 MessageID lastid;
00047                 Microthread *waiter;
00052                 MessageID genid();
00058                 void notify();
00059         public:
00065                 ChannelManager();
00066                 friend class BaseChannel;
00067                 friend class ChannelGroup;
00068                 template<class T> friend class Channel;
00069 };
00070 
00075 class BaseChannel {
00076         protected:
00077                 ChannelManager &manager;
00078                 MessageID messageid;
00083                 BaseChannel(ChannelManager &m);
00089                 MessageID nextid() const;
00096                 struct MSGIDComparator
00097                                 : public std::binary_function<BaseChannel,
00098                                                 BaseChannel, bool> {
00102                         bool operator()(const BaseChannel& a,
00103                                 const BaseChannel &b) const;
00104                 };
00105                 void waitformessage();
00106         public:
00107                 friend class ChannelGroup;
00108 };
00109 
00114 template<class T> class Channel : public BaseChannel {
00115         private:
00116                 std::queue<std::pair<MessageID, T>*> queue;
00117         public:
00122                 Channel(ChannelManager &m);
00128                 void send(T data);
00135                 T receive();
00141                 bool isempty() const;
00142 };
00143 
00151 class ChannelGroup {
00152         private:
00153                 ChannelManager &manager;
00154                 std::set<BaseChannel*> channels;
00155                 BaseChannel *unlockedtryselect() const;
00156         public:
00161                 ChannelGroup(ChannelManager &m);
00167                 void addChannel(BaseChannel &chan);
00173                 void removeChannel(BaseChannel &chan);
00186                 BaseChannel *tryselect() const;
00194                 BaseChannel &select() const;
00195 };
00196 
00197 #endif

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