SUMO - Simulation of Urban MObility
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-2017 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 #define NOMINMAX
35 #include <xincs.h>
36 #undef NOMINMAX
37 #include <fxdefs.h>
38 
39 using namespace FX;
40 
41 #include "MFXMutex.h"
42 
43 #ifndef WIN32
44 #include <pthread.h>
45 #endif
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  (void)status; // only used for assertion
59  pthread_mutexattr_destroy(&attr);
60 #else
61  mutexHandle = CreateMutex(NULL, FALSE, NULL);
62  FXASSERT(mutexHandle != NULL);
63 #endif
64 }
65 
66 // Note: lock_ is not safe here because it is not protected, but
67 // if you are causing the destructor to be executed while
68 // some other thread is accessing the mutexHandle, then you have
69 // a design flaw in your program, and so it should crash!
71  if (lock_) {
72  fxerror("MFXMutex: mutex still locked\n");
73  }
74 #if !defined(WIN32)
75  pthread_mutex_destroy((pthread_mutex_t*)mutexHandle);
76  FXFREE(&mutexHandle);
77 #else
78  CloseHandle(mutexHandle);
79 #endif
80 }
81 
82 // lock_ is safe because we dont increment it until we
83 // have entered the locked state - cha-ching, correct
85 #if !defined(WIN32)
86  pthread_mutex_lock((pthread_mutex_t*)mutexHandle);
87 #else
88  WaitForSingleObject(mutexHandle, INFINITE);
89 #endif
90  lock_++;
91 }
92 
93 // lock_ is safe because we decrement it, before leaving the locked state
95  lock_--;
96 #if !defined(WIN32)
97  pthread_mutex_unlock((pthread_mutex_t*)mutexHandle);
98 #else
99  ReleaseMutex(mutexHandle);
100 #endif
101 }
102 
FXuint lock_
Definition: MFXMutex.h:75
#define INFINITE
Definition: fxexdefs.h:93
FXThreadMutex mutexHandle
Definition: MFXMutex.h:78
virtual ~MFXMutex()
dtor
Definition: MFXMutex.cpp:70
void unlock()
release mutex lock
Definition: MFXMutex.cpp:94
void lock()
lock mutex
Definition: MFXMutex.cpp:84
MFXMutex()
create me a mutex :-)
Definition: MFXMutex.cpp:48