MFXMutex.cpp

Go to the documentation of this file.
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_) fxerror("MFXMutex: mutex still locked\n");
00071 #if !defined(WIN32)
00072     pthread_mutex_destroy((pthread_mutex_t*)mutexHandle);
00073     FXFREE(&mutexHandle);
00074 #else
00075     CloseHandle(mutexHandle);
00076 #endif
00077 }
00078 
00079 // lock_ is safe because we dont increment it until we
00080 // have entered the locked state - cha-ching, correct
00081 void MFXMutex::lock() {
00082 #if !defined(WIN32)
00083     pthread_mutex_lock((pthread_mutex_t*)mutexHandle);
00084 #else
00085     WaitForSingleObject(mutexHandle,INFINITE);
00086 #endif
00087     lock_++;
00088 }
00089 
00090 // lock_ is safe because we decrement it, before leaving the locked state
00091 void MFXMutex::unlock() {
00092     lock_--;
00093 #if !defined(WIN32)
00094     pthread_mutex_unlock((pthread_mutex_t*)mutexHandle);
00095 #else
00096     ReleaseMutex(mutexHandle);
00097 #endif
00098 }
00099 

Generated on Wed May 5 00:06:30 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6