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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 //
18 /****************************************************************************/
19 
20 
21 /* =========================================================================
22  * included modules
23  * ======================================================================= */
24 #include <config.h>
25 
26 #include <fxver.h>
27 #define NOMINMAX
28 #include <xincs.h>
29 #undef NOMINMAX
30 #include <fxdefs.h>
31 
32 using namespace FX;
33 
34 #include "MFXMutex.h"
35 
36 #ifndef WIN32
37 #include <pthread.h>
38 #endif
39 
40 // MFXMutex constructor
41 MFXMutex::MFXMutex() : lock_(0) {
42 #ifndef WIN32
43  FXint status = 0;
44  pthread_mutexattr_t attr;
45  pthread_mutexattr_init(&attr);
46  status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
47  FXASSERT(status == 0);
48  FXMALLOC(&mutexHandle, pthread_mutex_t, 1);
49  status = pthread_mutex_init((pthread_mutex_t*)mutexHandle, &attr);
50  FXASSERT(status == 0);
51  (void)status; // only used for assertion
52  pthread_mutexattr_destroy(&attr);
53 #else
54  mutexHandle = CreateMutex(nullptr, FALSE, nullptr);
55  FXASSERT(mutexHandle != NULL);
56 #endif
57 }
58 
59 // Note: lock_ is not safe here because it is not protected, but
60 // if you are causing the destructor to be executed while
61 // some other thread is accessing the mutexHandle, then you have
62 // a design flaw in your program, and so it should crash!
64  if (lock_) {
65  fxerror("MFXMutex: mutex still locked\n");
66  }
67 #if !defined(WIN32)
68  pthread_mutex_destroy((pthread_mutex_t*)mutexHandle);
69  FXFREE(&mutexHandle);
70 #else
71  CloseHandle(mutexHandle);
72 #endif
73 }
74 
75 // lock_ is safe because we dont increment it until we
76 // have entered the locked state - cha-ching, correct
78 #if !defined(WIN32)
79  pthread_mutex_lock((pthread_mutex_t*)mutexHandle);
80 #else
81  WaitForSingleObject(mutexHandle, INFINITE);
82 #endif
83  lock_++;
84 }
85 
86 // lock_ is safe because we decrement it, before leaving the locked state
88  lock_--;
89 #if !defined(WIN32)
90  pthread_mutex_unlock((pthread_mutex_t*)mutexHandle);
91 #else
92  ReleaseMutex(mutexHandle);
93 #endif
94 }
95 
FXuint lock_
lock count
Definition: MFXMutex.h:74
#define INFINITE
Definition: fxexdefs.h:86
FXThreadMutex mutexHandle
mutex handler
Definition: MFXMutex.h:78
virtual ~MFXMutex()
destructor
Definition: MFXMutex.cpp:63
void unlock()
release mutex lock
Definition: MFXMutex.cpp:87
void lock()
lock mutex
Definition: MFXMutex.cpp:77
MFXMutex()
constructor
Definition: MFXMutex.cpp:41