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         public:
00106                 friend class ChannelGroup;
00107 };
00108 
00113 template<class T> class Channel : public BaseChannel {
00114         private:
00115                 std::queue<std::pair<MessageID, T>*> queue;
00116         public:
00121                 Channel(ChannelManager &m);
00127                 void send(T data);
00134                 T receive();
00140                 bool isempty() const;
00141 };
00142 
00150 class ChannelGroup {
00151         private:
00152                 ChannelManager &manager;
00153                 std::set<BaseChannel*> channels;
00154                 BaseChannel *unlockedtryselect() const;
00155         public:
00160                 ChannelGroup(ChannelManager &m);
00166                 void addChannel(BaseChannel &chan);
00172                 void removeChannel(BaseChannel &chan);
00185                 BaseChannel *tryselect() const;
00193                 BaseChannel &select() const;
00194 };
00195 
00196 #endif

Generated on Thu Feb 14 13:30:30 2008 by  doxygen 1.5.1