00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "fsMutex.h"
00008
00009 #ifdef _DEBUG
00010 #undef THIS_FILE
00011 static char THIS_FILE[]=__FILE__;
00012 #define new DEBUG_NEW
00013 #endif
00014
00015 fsMutex::fsMutex()
00016 {
00017 m_mx = CreateMutex (NULL, FALSE, NULL);
00018 m_cLocks = 0;
00019 }
00020
00021 fsMutex::~fsMutex()
00022 {
00023 if (m_mx)
00024 CloseHandle (m_mx);
00025 }
00026
00027 void fsMutex::Lock()
00028 {
00029 ASSERT (m_mx != NULL);
00030
00031 if (m_mx == NULL)
00032 return;
00033
00034 const int WAIT_TIME = 10000;
00035 const int WAIT_INTERVAL = 50;
00036
00037 for (int i = 0; i < WAIT_TIME/WAIT_INTERVAL; i++)
00038 {
00039 DWORD dwRet = WaitForSingleObject (m_mx, WAIT_INTERVAL);
00040 if (dwRet == WAIT_TIMEOUT)
00041 {
00042 MSG msg;
00043 while (PeekMessage (&msg, 0, 0, 0, PM_REMOVE))
00044 {
00045 TranslateMessage (&msg);
00046 DispatchMessage (&msg);
00047 }
00048 }
00049 else
00050 {
00051
00052 m_cLocks++;
00053 break;
00054 }
00055 }
00056
00057 if (i == WAIT_TIME/WAIT_INTERVAL)
00058 {
00059 ASSERT (FALSE);
00060 HANDLE h = m_mx;
00061 m_mx = CreateMutex (NULL, FALSE, NULL);
00062 CloseHandle (h);
00063 }
00064 }
00065
00066 void fsMutex::Unlock()
00067 {
00068 if (m_mx == NULL || m_cLocks == 0)
00069 return;
00070
00071 m_cLocks--;
00072 ReleaseMutex (m_mx);
00073 }