sigx++ 2.0.1
dispatcher.h
Go to the documentation of this file.
00001 #ifndef _SIGX_DISPATCHER_HPP
00002 #define _SIGX_DISPATCHER_HPP
00003 
00004 /*
00005  * Copyright 2005 Tim Mayberry and Klaus Triendl
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Library General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Library General Public
00018  * License along with this library; if not, write to the Free 
00019  * Software Foundation, 51 Franklin Street, Fifth Floor, 
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00027 #include <queue>
00028 #include <map>
00029 #include <glib/gtypes.h> // gint
00030 #include <sigxconfig.h>
00031 #include <sigx/fwddecl.h>
00032 #include <sigx/bad_caller.h>
00033 #include <sigx/bad_sync_call.h>
00034 #include <sigx/operator_new.h>
00035 #include <sigx/glib_lockables.h>
00036 
00037 
00038 namespace sigx
00039 {
00040 
00041 typedef const void* threadhandle_type;
00042 
00043 
00044     namespace dld
00045     {
00046 
00050 typedef std::pair<const threadhandle_type /*threadA*/, const threadhandle_type /*threadB*/> thread_pair_type;
00051 
00056 thread_pair_type make_thread_pair(threadhandle_type threadA, threadhandle_type threadB);
00057 
00058 
00061 class syncmessages_counter
00062 {
00063 public:
00068     syncmessages_counter(const threadhandle_type& threadA);
00069 
00070 
00071 public:
00075     syncmessages_counter& operator ++();
00076 
00080     syncmessages_counter& operator --();
00081     
00086     operator bool() const;
00087 
00088 
00089 private:
00090     const threadhandle_type m_threadA;
00091     int m_countThreadA;
00092     int m_countThreadB;
00093 };
00094 
00095 struct thread_compare: public std::binary_function<thread_pair_type, thread_pair_type, bool>
00096 {
00097     bool operator ()(const thread_pair_type& threadpair1, const thread_pair_type& threadpair2) const
00098     {
00099         if (threadpair1.first < threadpair2.first)
00100             return true;
00101         if (threadpair1.first > threadpair2.first)
00102             return false;
00103         if (threadpair1.second < threadpair2.second)
00104             return true;
00105         //if (threadpair1.second > threadpair2.second)
00106         //  return false;
00107         return false;
00108     }
00109 };
00110 
00111 
00112 typedef std::map<thread_pair_type, syncmessages_counter, thread_compare> sync_messages_type;
00113 typedef static_mutex_lockable<sync_messages_type> lockable_sync_messages_type;
00114 
00115 
00116     } // namespace dld
00117 
00118 
00119 // fwd decl
00120 class tunnel_context_base;
00121 
00122 
00140 class SIGX_API dispatcher: public operator_new
00141 {
00142 public:
00156     static bool deadlock_detection;
00157 
00158 
00161     dispatcher();
00162 
00166     virtual ~dispatcher() = 0;
00167     
00170     virtual void send(tunnel_context_base* context);
00171 
00174     gint queued_contexts() const;
00175 
00176     threadhandle_type creator_thread() const { return m_creator_thread; }
00177 
00178 protected:
00182     bool process_next();
00183 
00187     void test_calling_thread();
00188 
00189 
00190 private:
00191     typedef std::queue<tunnel_context_base*> context_container_type;
00192     typedef mutex_lockable<context_container_type> lockable_tunnel_contexts;
00193     lockable_tunnel_contexts m_tunnel_contexts;
00194     
00201     volatile gint m_contexts_count;
00202     const threadhandle_type m_creator_thread;
00203 
00204 
00205 // deadlock detection
00206 private:
00217     static void increase_sync_messages(const dld::thread_pair_type& threadpair);
00218 
00224     static void decrease_sync_messages(const dld::thread_pair_type& threadpair);
00225 
00226     static dld::lockable_sync_messages_type thread_paired_sync_messages;
00227 };
00228 
00229 
00230 } // namespace sigx
00231 
00232 
00233 #endif // end file guard