SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MFXMutex.cpp
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * Mutal exclusion object (required for threads) *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2003 by Mathew Robertson. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 ********************************************************************************/
22 /* =========================================================================
23  * included modules
24  * ======================================================================= */
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <fxver.h>
32 #include <xincs.h>
33 #include <fxdefs.h>
34 
35 using namespace FX;
36 
37 #include "MFXMutex.h"
38 
39 #ifndef WIN32
40 #include <pthread.h>
41 #endif
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 // MFXMutex constructor
48 MFXMutex::MFXMutex() : lock_(0) {
49 #ifndef WIN32
50  FXint status = 0;
51  pthread_mutexattr_t attr;
52  pthread_mutexattr_init(&attr);
53  status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
54  FXASSERT(status == 0);
55  FXMALLOC(&mutexHandle, pthread_mutex_t, 1);
56  status = pthread_mutex_init((pthread_mutex_t*)mutexHandle, &attr);
57  FXASSERT(status == 0);
58  pthread_mutexattr_destroy(&attr);
59 #else
60  mutexHandle = CreateMutex(NULL, FALSE, NULL);
61  FXASSERT(mutexHandle != NULL);
62 #endif
63 }
64 
65 // Note: lock_ is not safe here because it is not protected, but
66 // if you are causing the destructor to be executed while
67 // some other thread is accessing the mutexHandle, then you have
68 // a design flaw in your program, and so it should crash!
70  if (lock_) {
71  fxerror("MFXMutex: mutex still locked\n");
72  }
73 #if !defined(WIN32)
74  pthread_mutex_destroy((pthread_mutex_t*)mutexHandle);
75  FXFREE(&mutexHandle);
76 #else
77  CloseHandle(mutexHandle);
78 #endif
79 }
80 
81 // lock_ is safe because we dont increment it until we
82 // have entered the locked state - cha-ching, correct
84 #if !defined(WIN32)
85  pthread_mutex_lock((pthread_mutex_t*)mutexHandle);
86 #else
87  WaitForSingleObject(mutexHandle, INFINITE);
88 #endif
89  lock_++;
90 }
91 
92 // lock_ is safe because we decrement it, before leaving the locked state
94  lock_--;
95 #if !defined(WIN32)
96  pthread_mutex_unlock((pthread_mutex_t*)mutexHandle);
97 #else
98  ReleaseMutex(mutexHandle);
99 #endif
100 }
101