SUMO - Simulation of Urban MObility
|
00001 /******************************************************************************** 00002 * * 00003 * Mutal exclusion object (required for threads) * 00004 * * 00005 ********************************************************************************* 00006 * Copyright (C) 2003 by Mathew Robertson. All Rights Reserved. * 00007 ********************************************************************************* 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 00021 ********************************************************************************/ 00022 /* ========================================================================= 00023 * included modules 00024 * ======================================================================= */ 00025 #ifdef _MSC_VER 00026 #include <windows_config.h> 00027 #else 00028 #include <config.h> 00029 #endif 00030 00031 #include <fxver.h> 00032 #include <xincs.h> 00033 #include <fxdefs.h> 00034 00035 using namespace FX; 00036 00037 #include "MFXMutex.h" 00038 00039 #ifndef WIN32 00040 #include <pthread.h> 00041 #endif 00042 00043 #ifdef CHECK_MEMORY_LEAKS 00044 #include <foreign/nvwa/debug_new.h> 00045 #endif // CHECK_MEMORY_LEAKS 00046 00047 // MFXMutex constructor 00048 MFXMutex::MFXMutex() : lock_(0) { 00049 #ifndef WIN32 00050 FXint status = 0; 00051 pthread_mutexattr_t attr; 00052 pthread_mutexattr_init(&attr); 00053 status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 00054 FXASSERT(status == 0); 00055 FXMALLOC(&mutexHandle, pthread_mutex_t, 1); 00056 status = pthread_mutex_init((pthread_mutex_t*)mutexHandle, &attr); 00057 FXASSERT(status == 0); 00058 pthread_mutexattr_destroy(&attr); 00059 #else 00060 mutexHandle = CreateMutex(NULL, FALSE, NULL); 00061 FXASSERT(mutexHandle != NULL); 00062 #endif 00063 } 00064 00065 // Note: lock_ is not safe here because it is not protected, but 00066 // if you are causing the destructor to be executed while 00067 // some other thread is accessing the mutexHandle, then you have 00068 // a design flaw in your program, and so it should crash! 00069 MFXMutex::~MFXMutex() { 00070 if (lock_) { 00071 fxerror("MFXMutex: mutex still locked\n"); 00072 } 00073 #if !defined(WIN32) 00074 pthread_mutex_destroy((pthread_mutex_t*)mutexHandle); 00075 FXFREE(&mutexHandle); 00076 #else 00077 CloseHandle(mutexHandle); 00078 #endif 00079 } 00080 00081 // lock_ is safe because we dont increment it until we 00082 // have entered the locked state - cha-ching, correct 00083 void MFXMutex::lock() { 00084 #if !defined(WIN32) 00085 pthread_mutex_lock((pthread_mutex_t*)mutexHandle); 00086 #else 00087 WaitForSingleObject(mutexHandle, INFINITE); 00088 #endif 00089 lock_++; 00090 } 00091 00092 // lock_ is safe because we decrement it, before leaving the locked state 00093 void MFXMutex::unlock() { 00094 lock_--; 00095 #if !defined(WIN32) 00096 pthread_mutex_unlock((pthread_mutex_t*)mutexHandle); 00097 #else 00098 ReleaseMutex(mutexHandle); 00099 #endif 00100 } 00101