sigx++  2.0.1
dispatcher.h
Go to the documentation of this file.
1 #ifndef _SIGX_DISPATCHER_HPP
2 #define _SIGX_DISPATCHER_HPP
3 
4 /*
5  * Copyright 2005 Tim Mayberry and Klaus Triendl
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
27 #include <queue>
28 #include <map>
29 #include <glib.h> // gint
30 #include <sigxconfig.h>
31 #include <sigx/fwddecl.h>
32 #include <sigx/bad_caller.h>
33 #include <sigx/bad_sync_call.h>
34 #include <sigx/operator_new.h>
35 #include <sigx/glib_lockables.h>
36 
37 
38 namespace sigx
39 {
40 
41 typedef const void* threadhandle_type;
42 
43 
44  namespace dld
45  {
46 
50 typedef std::pair<const threadhandle_type /*threadA*/, const threadhandle_type /*threadB*/> thread_pair_type;
51 
57 
58 
62 {
63 public:
69 
70 
71 public:
76 
81 
86  operator bool() const;
87 
88 
89 private:
90  const threadhandle_type m_threadA;
91  int m_countThreadA;
92  int m_countThreadB;
93 };
94 
95 struct thread_compare: public std::binary_function<thread_pair_type, thread_pair_type, bool>
96 {
97  bool operator ()(const thread_pair_type& threadpair1, const thread_pair_type& threadpair2) const
98  {
99  if (threadpair1.first < threadpair2.first)
100  return true;
101  if (threadpair1.first > threadpair2.first)
102  return false;
103  if (threadpair1.second < threadpair2.second)
104  return true;
105  //if (threadpair1.second > threadpair2.second)
106  // return false;
107  return false;
108  }
109 };
110 
111 
112 typedef std::map<thread_pair_type, syncmessages_counter, thread_compare> sync_messages_type;
114 
115 
116  } // namespace dld
117 
118 
119 // fwd decl
120 class tunnel_context_base;
121 
122 
140 class SIGX_API dispatcher: public operator_new
141 {
142 public:
156  static bool deadlock_detection;
157 
158 
161  dispatcher();
162 
166  virtual ~dispatcher() = 0;
167 
170  virtual void send(tunnel_context_base* context);
171 
174  gint queued_contexts() const;
175 
176  threadhandle_type creator_thread() const { return m_creator_thread; }
177 
178 protected:
182  bool process_next();
183 
187  void test_calling_thread();
188 
189 
190 private:
191  typedef std::queue<tunnel_context_base*> context_container_type;
192  typedef mutex_lockable<context_container_type> lockable_tunnel_contexts;
193  lockable_tunnel_contexts m_tunnel_contexts;
194 
201  volatile gint m_contexts_count;
202  const threadhandle_type m_creator_thread;
203 
204 
205 // deadlock detection
206 private:
217  static void increase_sync_messages(const dld::thread_pair_type& threadpair);
218 
224  static void decrease_sync_messages(const dld::thread_pair_type& threadpair);
225 
226  static dld::lockable_sync_messages_type thread_paired_sync_messages;
227 };
228 
229 
230 } // namespace sigx
231 
232 
233 #endif // end file guard
the base class for all tunnel_context classes.
Definition: tunnel_context_base.h:42
std::pair< const threadhandle_type, const threadhandle_type > thread_pair_type
A pair of threads where pair::first is the smaller one and pair::second the greater one...
Definition: dispatcher.h:50
Ensures allocation of derived objects in the sigx module.
Definition: operator_new.h:33
syncmessages_counter(const threadhandle_type &threadA)
Construct a syncmessages_counter object.
Definition: dispatcher.cpp:181
Definition: dispatcher.h:95
thread_pair_type make_thread_pair(threadhandle_type threadA, threadhandle_type threadB)
Creates a pair of thread handles where the first pair::first is the smaller one of both and pair::sec...
Definition: dispatcher.cpp:172
syncmessages_counter & operator++()
Increase the count of synchronous messages to the server thread.
Definition: dispatcher.cpp:185
static_mutex_lockable< sync_messages_type > lockable_sync_messages_type
Definition: dispatcher.h:113
std::map< thread_pair_type, syncmessages_counter, thread_compare > sync_messages_type
Definition: dispatcher.h:112
Makes T_type lockable with a Glib::StaticMutex.
Definition: glib_lockables.h:76
threadhandle_type creator_thread() const
Definition: dispatcher.h:176
static bool deadlock_detection
Whether deadlock detection is turned on.
Definition: dispatcher.h:156
syncmessages_counter & operator--()
Decrease the count of synchronous messages to the server thread.
Definition: dispatcher.cpp:194
Holds a counter of synchronous messages between two threads.
Definition: dispatcher.h:61
bool operator()(const thread_pair_type &threadpair1, const thread_pair_type &threadpair2) const
Definition: dispatcher.h:97
base class denoting the ability to dispatch messages between threads.
Definition: dispatcher.h:140
const void * threadhandle_type
Definition: dispatcher.h:41