SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MFXMutex.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 //
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2007-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 /* =========================================================================
25  * included modules
26  * ======================================================================= */
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <fxver.h>
34 #include <xincs.h>
35 #include <fxdefs.h>
36 
37 using namespace FX;
38 
39 #include "MFXMutex.h"
40 
41 #ifndef WIN32
42 #include <pthread.h>
43 #endif
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 // MFXMutex constructor
50 MFXMutex::MFXMutex() : lock_(0) {
51 #ifndef WIN32
52  FXint status = 0;
53  pthread_mutexattr_t attr;
54  pthread_mutexattr_init(&attr);
55  status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
56  FXASSERT(status == 0);
57  FXMALLOC(&mutexHandle, pthread_mutex_t, 1);
58  status = pthread_mutex_init((pthread_mutex_t*)mutexHandle, &attr);
59  FXASSERT(status == 0);
60  (void)status; // only used for assertion
61  pthread_mutexattr_destroy(&attr);
62 #else
63  mutexHandle = CreateMutex(NULL, FALSE, NULL);
64  FXASSERT(mutexHandle != NULL);
65 #endif
66 }
67 
68 // Note: lock_ is not safe here because it is not protected, but
69 // if you are causing the destructor to be executed while
70 // some other thread is accessing the mutexHandle, then you have
71 // a design flaw in your program, and so it should crash!
73  if (lock_) {
74  fxerror("MFXMutex: mutex still locked\n");
75  }
76 #if !defined(WIN32)
77  pthread_mutex_destroy((pthread_mutex_t*)mutexHandle);
78  FXFREE(&mutexHandle);
79 #else
80  CloseHandle(mutexHandle);
81 #endif
82 }
83 
84 // lock_ is safe because we dont increment it until we
85 // have entered the locked state - cha-ching, correct
87 #if !defined(WIN32)
88  pthread_mutex_lock((pthread_mutex_t*)mutexHandle);
89 #else
90  WaitForSingleObject(mutexHandle, INFINITE);
91 #endif
92  lock_++;
93 }
94 
95 // lock_ is safe because we decrement it, before leaving the locked state
97  lock_--;
98 #if !defined(WIN32)
99  pthread_mutex_unlock((pthread_mutex_t*)mutexHandle);
100 #else
101  ReleaseMutex(mutexHandle);
102 #endif
103 }
104 
FXuint lock_
Definition: MFXMutex.h:75
#define INFINITE
Definition: fxexdefs.h:93
FXThreadMutex mutexHandle
Definition: MFXMutex.h:78
virtual ~MFXMutex()
dtor
Definition: MFXMutex.cpp:72
void unlock()
release mutex lock
Definition: MFXMutex.cpp:96
void lock()
lock mutex
Definition: MFXMutex.cpp:86
MFXMutex()
create me a mutex :-)
Definition: MFXMutex.cpp:50