SUMO - Simulation of Urban MObility
MFXMutex.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2007-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 //
20 /****************************************************************************/
21 
22 
23 /* =========================================================================
24  * included modules
25  * ======================================================================= */
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <fxver.h>
33 #define NOMINMAX
34 #include <xincs.h>
35 #undef NOMINMAX
36 #include <fxdefs.h>
37 
38 using namespace FX;
39 
40 #include "MFXMutex.h"
41 
42 #ifndef WIN32
43 #include <pthread.h>
44 #endif
45 
46 // MFXMutex constructor
47 MFXMutex::MFXMutex() : lock_(0) {
48 #ifndef WIN32
49  FXint status = 0;
50  pthread_mutexattr_t attr;
51  pthread_mutexattr_init(&attr);
52  status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
53  FXASSERT(status == 0);
54  FXMALLOC(&mutexHandle, pthread_mutex_t, 1);
55  status = pthread_mutex_init((pthread_mutex_t*)mutexHandle, &attr);
56  FXASSERT(status == 0);
57  (void)status; // only used for assertion
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 
FXuint lock_
Definition: MFXMutex.h:74
#define INFINITE
Definition: fxexdefs.h:92
FXThreadMutex mutexHandle
Definition: MFXMutex.h:77
virtual ~MFXMutex()
dtor
Definition: MFXMutex.cpp:69
void unlock()
release mutex lock
Definition: MFXMutex.cpp:93
void lock()
lock mutex
Definition: MFXMutex.cpp:83
MFXMutex()
create me a mutex :-)
Definition: MFXMutex.cpp:47