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 #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 // MFXMutex constructor
46 MFXMutex::MFXMutex() : lock_(0) {
47 #ifndef WIN32
48  FXint status = 0;
49  pthread_mutexattr_t attr;
50  pthread_mutexattr_init(&attr);
51  status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
52  FXASSERT(status == 0);
53  FXMALLOC(&mutexHandle, pthread_mutex_t, 1);
54  status = pthread_mutex_init((pthread_mutex_t*)mutexHandle, &attr);
55  FXASSERT(status == 0);
56  (void)status; // only used for assertion
57  pthread_mutexattr_destroy(&attr);
58 #else
59  mutexHandle = CreateMutex(NULL, FALSE, NULL);
60  FXASSERT(mutexHandle != NULL);
61 #endif
62 }
63 
64 // Note: lock_ is not safe here because it is not protected, but
65 // if you are causing the destructor to be executed while
66 // some other thread is accessing the mutexHandle, then you have
67 // a design flaw in your program, and so it should crash!
69  if (lock_) {
70  fxerror("MFXMutex: mutex still locked\n");
71  }
72 #if !defined(WIN32)
73  pthread_mutex_destroy((pthread_mutex_t*)mutexHandle);
74  FXFREE(&mutexHandle);
75 #else
76  CloseHandle(mutexHandle);
77 #endif
78 }
79 
80 // lock_ is safe because we dont increment it until we
81 // have entered the locked state - cha-ching, correct
83 #if !defined(WIN32)
84  pthread_mutex_lock((pthread_mutex_t*)mutexHandle);
85 #else
86  WaitForSingleObject(mutexHandle, INFINITE);
87 #endif
88  lock_++;
89 }
90 
91 // lock_ is safe because we decrement it, before leaving the locked state
93  lock_--;
94 #if !defined(WIN32)
95  pthread_mutex_unlock((pthread_mutex_t*)mutexHandle);
96 #else
97  ReleaseMutex(mutexHandle);
98 #endif
99 }
100 
FXuint lock_
Definition: MFXMutex.h:75
#define INFINITE
Definition: fxexdefs.h:93
FXThreadMutex mutexHandle
Definition: MFXMutex.h:78
virtual ~MFXMutex()
dtor
Definition: MFXMutex.cpp:68
void unlock()
release mutex lock
Definition: MFXMutex.cpp:92
void lock()
lock mutex
Definition: MFXMutex.cpp:82
MFXMutex()
create me a mutex :-)
Definition: MFXMutex.cpp:46