SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fast_mutex.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 #ifdef _MSC_VER
4 #include <windows_config.h>
5 #else
6 #include <config.h>
7 #endif
8 #ifdef CHECK_MEMORY_LEAKS
9 /*
10  * Copyright (C) 2004-2008 Wu Yongwei <adah at users dot sourceforge dot net>
11  *
12  * This software is provided 'as-is', without any express or implied
13  * warranty. In no event will the authors be held liable for any
14  * damages arising from the use of this software.
15  *
16  * Permission is granted to anyone to use this software for any purpose,
17  * including commercial applications, and to alter it and redistribute
18  * it freely, subject to the following restrictions:
19  *
20  * 1. The origin of this software must not be misrepresented; you must
21  * not claim that you wrote the original software. If you use this
22  * software in a product, an acknowledgement in the product
23  * documentation would be appreciated but is not required.
24  * 2. Altered source versions must be plainly marked as such, and must
25  * not be misrepresented as being the original software.
26  * 3. This notice may not be removed or altered from any source
27  * distribution.
28  *
29  * This file is part of Stones of Nvwa:
30  * http://sourceforge.net/projects/nvwa
31  *
32  */
33 
44 #ifndef _FAST_MUTEX_H
45 #define _FAST_MUTEX_H
46 
47 # if !defined(_NOTHREADS)
48 # if !defined(_WIN32THREADS) && \
49  (defined(_WIN32) && defined(_MT))
50 // Automatically use _WIN32THREADS when specifying -MT/-MD in MSVC,
51 // or -mthreads in MinGW GCC.
52 # define _WIN32THREADS
53 # elif !defined(_PTHREADS) && \
54  defined(_REENTRANT)
55 // Automatically use _PTHREADS when specifying -pthread in GCC.
56 // N.B. I do not detect on _PTHREAD_H since libstdc++-v3 under
57 // Linux will silently include <pthread.h> anyway.
58 # define _PTHREADS
59 # endif
60 # endif
61 
62 # if !defined(_PTHREADS) && !defined(_WIN32THREADS) && !defined(_NOTHREADS)
63 # define _NOTHREADS
64 # endif
65 
66 # if defined(_NOTHREADS)
67 # if defined(_PTHREADS) || defined(_WIN32THREADS)
68 # undef _NOTHREADS
69 # error "Cannot define multi-threaded mode with -D_NOTHREADS"
70 # if defined(__MINGW32__) && defined(_WIN32THREADS) && !defined(_MT)
71 # error "Be sure to specify -mthreads with -D_WIN32THREADS"
72 # endif
73 # endif
74 # endif
75 
76 # ifndef _FAST_MUTEX_CHECK_INITIALIZATION
77 
85 # define _FAST_MUTEX_CHECK_INITIALIZATION 1
86 # endif
87 
88 # if defined(_PTHREADS) && defined(_WIN32THREADS)
89 // Some C++ libraries have _PTHREADS defined even on Win32 platforms.
90 // Thus this hack.
91 # undef _PTHREADS
92 # endif
93 
94 # ifdef _DEBUG
95 # include <stdio.h>
96 # include <stdlib.h>
98 # define _FAST_MUTEX_ASSERT(_Expr, _Msg) \
99  if (!(_Expr)) { \
100  fprintf(stderr, "fast_mutex::%s\n", _Msg); \
101  abort(); \
102  }
103 # else
104 
105 # define _FAST_MUTEX_ASSERT(_Expr, _Msg) \
106  ((void)0)
107 # endif
108 
109 # ifdef _PTHREADS
110 # include <pthread.h>
115 # define __VOLATILE volatile
116 
120  class fast_mutex
121  {
122  pthread_mutex_t _M_mtx_impl;
123 # if _FAST_MUTEX_CHECK_INITIALIZATION
124  bool _M_initialized;
125 # endif
126 # ifdef _DEBUG
127  bool _M_locked;
128 # endif
129  public:
130  fast_mutex()
131 # ifdef _DEBUG
132  : _M_locked(false)
133 # endif
134  {
135  ::pthread_mutex_init(&_M_mtx_impl, NULL);
136 # if _FAST_MUTEX_CHECK_INITIALIZATION
137  _M_initialized = true;
138 # endif
139  }
140  ~fast_mutex()
141  {
142  _FAST_MUTEX_ASSERT(!_M_locked, "~fast_mutex(): still locked");
143 # if _FAST_MUTEX_CHECK_INITIALIZATION
144  _M_initialized = false;
145 # endif
146  ::pthread_mutex_destroy(&_M_mtx_impl);
147  }
148  void lock()
149  {
150 # if _FAST_MUTEX_CHECK_INITIALIZATION
151  if (!_M_initialized)
152  return;
153 # endif
154  ::pthread_mutex_lock(&_M_mtx_impl);
155 # ifdef _DEBUG
156  // The following assertion should _always_ be true for a
157  // real `fast' pthread_mutex. However, this assertion can
158  // help sometimes, when people forget to use `-lpthread' and
159  // glibc provides an empty implementation. Having this
160  // assertion is also more consistent.
161  _FAST_MUTEX_ASSERT(!_M_locked, "lock(): already locked");
162  _M_locked = true;
163 # endif
164  }
165  void unlock()
166  {
167 # if _FAST_MUTEX_CHECK_INITIALIZATION
168  if (!_M_initialized)
169  return;
170 # endif
171 # ifdef _DEBUG
172  _FAST_MUTEX_ASSERT(_M_locked, "unlock(): not locked");
173  _M_locked = false;
174 # endif
175  ::pthread_mutex_unlock(&_M_mtx_impl);
176  }
177  private:
178  fast_mutex(const fast_mutex&);
179  fast_mutex& operator=(const fast_mutex&);
180  };
181 # endif // _PTHREADS
182 
183 # ifdef _WIN32THREADS
184 # include <windows.h>
189 # define __VOLATILE volatile
190 
194  class fast_mutex
195  {
196  CRITICAL_SECTION _M_mtx_impl;
197 # if _FAST_MUTEX_CHECK_INITIALIZATION
198  bool _M_initialized;
199 # endif
200 # ifdef _DEBUG
201  bool _M_locked;
202 # endif
203  public:
204  fast_mutex()
205 # ifdef _DEBUG
206  : _M_locked(false)
207 # endif
208  {
209  ::InitializeCriticalSection(&_M_mtx_impl);
210 # if _FAST_MUTEX_CHECK_INITIALIZATION
211  _M_initialized = true;
212 # endif
213  }
214  ~fast_mutex()
215  {
216  _FAST_MUTEX_ASSERT(!_M_locked, "~fast_mutex(): still locked");
217 # if _FAST_MUTEX_CHECK_INITIALIZATION
218  _M_initialized = false;
219 # endif
220  ::DeleteCriticalSection(&_M_mtx_impl);
221  }
222  void lock()
223  {
224 # if _FAST_MUTEX_CHECK_INITIALIZATION
225  if (!_M_initialized)
226  return;
227 # endif
228  ::EnterCriticalSection(&_M_mtx_impl);
229 # ifdef _DEBUG
230  _FAST_MUTEX_ASSERT(!_M_locked, "lock(): already locked");
231  _M_locked = true;
232 # endif
233  }
234  void unlock()
235  {
236 # if _FAST_MUTEX_CHECK_INITIALIZATION
237  if (!_M_initialized)
238  return;
239 # endif
240 # ifdef _DEBUG
241  _FAST_MUTEX_ASSERT(_M_locked, "unlock(): not locked");
242  _M_locked = false;
243 # endif
244  ::LeaveCriticalSection(&_M_mtx_impl);
245  }
246  private:
247  fast_mutex(const fast_mutex&);
248  fast_mutex& operator=(const fast_mutex&);
249  };
250 # endif // _WIN32THREADS
251 
252 # ifdef _NOTHREADS
253 
257 # define __VOLATILE
258 
262  class fast_mutex
263  {
264 # ifdef _DEBUG
265  bool _M_locked;
266 # endif
267  public:
268  fast_mutex()
269 # ifdef _DEBUG
270  : _M_locked(false)
271 # endif
272  {
273  }
274  ~fast_mutex()
275  {
276  _FAST_MUTEX_ASSERT(!_M_locked, "~fast_mutex(): still locked");
277  }
278  void lock()
279  {
280 # ifdef _DEBUG
281  _FAST_MUTEX_ASSERT(!_M_locked, "lock(): already locked");
282  _M_locked = true;
283 # endif
284  }
285  void unlock()
286  {
287 # ifdef _DEBUG
288  _FAST_MUTEX_ASSERT(_M_locked, "unlock(): not locked");
289  _M_locked = false;
290 # endif
291  }
292  private:
293  fast_mutex(const fast_mutex&);
294  fast_mutex& operator=(const fast_mutex&);
295  };
296 # endif // _NOTHREADS
297 
299 class fast_mutex_autolock
300 {
301  fast_mutex& _M_mtx;
302 public:
303  explicit fast_mutex_autolock(fast_mutex& __mtx) : _M_mtx(__mtx)
304  {
305  _M_mtx.lock();
306  }
307  ~fast_mutex_autolock()
308  {
309  _M_mtx.unlock();
310  }
311 private:
312  fast_mutex_autolock(const fast_mutex_autolock&);
313  fast_mutex_autolock& operator=(const fast_mutex_autolock&);
314 };
315 
316 #endif // _FAST_MUTEX_H
317 
318 
319 #endif
320