• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

SimpleMutex.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include "SimpleMutex.h"
00009 #include "RakAssert.h"
00010 
00011 SimpleMutex::SimpleMutex() //: isInitialized(false)
00012 {
00013         // Prior implementation of Initializing in Lock() was not threadsafe
00014         Init();
00015 }
00016 
00017 SimpleMutex::~SimpleMutex()
00018 {
00019 //      if (isInitialized==false)
00020 //              return;
00021 #ifdef _WIN32
00022         //      CloseHandle(hMutex);
00023         DeleteCriticalSection(&criticalSection);
00024 #else
00025         pthread_mutex_destroy(&hMutex);
00026 #endif
00027 }
00028 
00029 #ifdef _WIN32
00030 #ifdef _DEBUG
00031 #include <stdio.h>
00032 #endif
00033 #endif
00034 
00035 void SimpleMutex::Lock(void)
00036 {
00037 //      if (isInitialized==false)
00038 //              Init();
00039 
00040 #ifdef _WIN32
00041         /*
00042         DWORD d = WaitForSingleObject(hMutex, INFINITE);
00043         #ifdef _DEBUG
00044         if (d==WAIT_FAILED)
00045         {
00046         LPVOID messageBuffer;
00047         FormatMessage(
00048         FORMAT_MESSAGE_ALLOCATE_BUFFER |
00049         FORMAT_MESSAGE_FROM_SYSTEM |
00050         FORMAT_MESSAGE_IGNORE_INSERTS,
00051         NULL,
00052         GetLastError(),
00053         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
00054         (LPTSTR) &messageBuffer,
00055         0,
00056         NULL
00057         );
00058         // Process any inserts in messageBuffer.
00059         // ...
00060         // Display the string.
00061         //MessageBox( NULL, (LPCTSTR)messageBuffer, "Error", MB_OK | MB_ICONINFORMATION );
00062         RAKNET_DEBUG_PRINTF("SimpleMutex error: %s", messageBuffer);
00063         // Free the buffer.
00064         LocalFree( messageBuffer );
00065 
00066         }
00067 
00068         RakAssert(d==WAIT_OBJECT_0);
00069         */
00070         EnterCriticalSection(&criticalSection);
00071 
00072 #else
00073         int error = pthread_mutex_lock(&hMutex);
00074         (void) error;
00075         RakAssert(error==0);
00076 #endif
00077 }
00078 
00079 void SimpleMutex::Unlock(void)
00080 {
00081 //      if (isInitialized==false)
00082 //              return;
00083 #ifdef _WIN32
00084         //      ReleaseMutex(hMutex);
00085         LeaveCriticalSection(&criticalSection);
00086 #else
00087         int error = pthread_mutex_unlock(&hMutex);
00088         (void) error;
00089         RakAssert(error==0);
00090 #endif
00091 }
00092 
00093 void SimpleMutex::Init(void)
00094 {
00095 #ifdef _WIN32
00096         //      hMutex = CreateMutex(NULL, FALSE, 0);
00097         //      RakAssert(hMutex);
00098         InitializeCriticalSection(&criticalSection);
00099 #else
00100         int error = pthread_mutex_init(&hMutex, 0);
00101         (void) error;
00102         RakAssert(error==0);
00103 #endif
00104 //      isInitialized=true;
00105 }

Generated on Thu Sep 30 2010 01:27:28 for RakNet by  doxygen 1.7.1