MFXMutex.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
00066
00067
00068
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
00080
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
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