BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
mutex.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_SYSTEM_MUTEX_H
6 #define BALL_SYSTEM_MUTEX_H
7 
8 #ifndef BALL_COMMON_GLOBAL_H
9 # include <BALL/COMMON/global.h>
10 #endif
11 
12 #include <QtCore/QMutex>
13 #include <QtCore/QReadWriteLock>
14 #include <QtCore/QMutexLocker>
15 #include <QtCore/QReadLocker>
16 #include <QtCore/QWriteLocker>
17 
18 #ifdef BALL_HAS_BOOST_THREAD
19 # include <boost/version.hpp>
20 # if BOOST_VERSION >= 103500
21 # include <boost/thread/mutex.hpp>
22 # define BALL_HAS_BOOST_MUTEX
23 # endif
24 #endif
25 
26 #undef BALL_USE_THREAD_CHECKER_API
27 #ifdef BALL_USE_THREAD_CHECKER_API
28 # include <libittnotify.h>
29 #endif
30 
31 #define BALL_DEFAULT_MUTEX_TYPE QMutex
32 #define BALL_DEFAULT_MUTEXLOCKER_TYPE QMutexLocker
33 #define BALL_DEFAULT_READWRITELOCK_TYPE QReadWriteLock
34 #define BALL_DEFAULT_READLOCKER_TYPE QReadLocker
35 #define BALL_DEFAULT_WRITELOCKER_TYPE QWriteLocker
36 
37 namespace BALL
38 {
46  template <class MutexType>
47  class TMutex
48  : public MutexType
49  {
50  public:
51  TMutex(bool is_recursive = false)
52  : MutexType()
53  {}
54  };
55 
56  template <>
57  class TMutex<QMutex>
58  : public QMutex
59  {
60  public:
61  TMutex(bool is_recursive = false)
62  : QMutex( is_recursive ? Recursive : NonRecursive )
63  {
64  }
65  };
66 
77  template <class MutexLockerType, class MutexType>
79  : public MutexLockerType
80  {
81  public:
83  : MutexLockerType(mutex)
84  {}
85  };
86 
95  template <class ReadWriteLockType>
97  : public ReadWriteLockType
98  {
99  public:
100  TReadWriteLock(bool is_recursive = false)
102  {}
103  };
104 
105  template <>
106  class TReadWriteLock<QReadWriteLock>
107  : public QReadWriteLock
108  {
109  public:
110  TReadWriteLock(bool is_recursive = false)
111 // NOTE: Qt only supports non-recursive read write locks since version 4.4. Before that, they were always recursive.
112 #if (QT_VERSION >= QT_VERSION_CHECK(4, 4, 0))
113  : QReadWriteLock( is_recursive ? Recursive : NonRecursive )
114 #else
115  : QReadWriteLock()
116 #endif
117  {
118  }
119  };
120 
131  template <class ReadLockerType, class ReadWriteLockType>
133  : public ReadLockerType
134  {
135  public:
137  : ReadLockerType(lock)
138  {}
139  };
140 
151  template <class WriteLockerType, class ReadWriteLockType>
153  : public WriteLockerType
154  {
155  public:
157  : WriteLockerType(lock)
158  {}
159  };
160 
161 
162 
163 #ifdef BALL_HAS_BOOST_MUTEX
164  // Boost-based mutexes only require a mapping of tryLock to try_lock.
165  template <>
166  class TMutex<boost::mutex>
167  : public boost::mutex
168  {
169  public:
170  TMutex()
171  : boost::mutex()
172  {}
173 
174  void lock()
175  {
176  return boost::mutex::lock();
177  }
178 
179  bool tryLock()
180  {
181  return try_lock();
182  }
183  };
184 
185 #endif
186 
187 
188 #ifdef BALL_USE_THREAD_CHECKER_API
189  template <>
190  class TMutex<QMutex>
191  : public QMutex
192  {
193  public:
194  TMutex()
195  : QMutex()
196  {
197  }
198 
199  void lock()
200  {
201  __itt_notify_sync_prepare((void *)this);
202  QMutex::lock();
203  __itt_notify_sync_acquired((void*)this);
204  }
205 
206  void unlock()
207  {
208  __itt_notify_sync_releasing((void *)this);
209  QMutex::unlock();
210  }
211 
212  bool tryLock()
213  {
214  __itt_notify_sync_prepare((void*)this);
215  bool result = QMutex::tryLock();
216 
217  if (result)
218  __itt_notify_sync_acquired((void*)this);
219  else
220  __itt_notify_sync_cancel((void*)this);
221 
222  return result;
223  }
224  };
225 
226  // TODO: similar instantiation for TReadWriteLock
227 
228 #else
229 // required for visual studio
230 #ifdef BALL_COMPILER_MSVC
231  //template class BALL_EXPORT TMutex<BALL_DEFAULT_MUTEX_TYPE>;
232  //template class BALL_EXPORT TReadWriteLock<BALL_DEFAULT_READWRITELOCK_TYPE>;
233  template class BALL_EXPORT TMutexLocker<BALL_DEFAULT_MUTEXLOCKER_TYPE, BALL_DEFAULT_MUTEX_TYPE>;
234  template class BALL_EXPORT TReadLocker<BALL_DEFAULT_READLOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE>;
235  template class BALL_EXPORT TWriteLocker<BALL_DEFAULT_WRITELOCKER_TYPE, BALL_DEFAULT_READWRITELOCK_TYPE>;
236 #endif
237 #endif
238 
239  // the standard mutex to use
245 }
246 
247 #endif // BALL_SYSTEM_MUTEX_H
248