diff --git a/LEGO1/mxcriticalsection.cpp b/LEGO1/mxcriticalsection.cpp new file mode 100644 index 00000000..9b25c8f9 --- /dev/null +++ b/LEGO1/mxcriticalsection.cpp @@ -0,0 +1,73 @@ +#include "mxcriticalsection.h" + +#include + +int g_useMutex = 0; + +MxCriticalSection::MxCriticalSection() +{ + HANDLE mutex; + + if (g_useMutex != 0) + { + mutex = CreateMutexA(NULL, FALSE, NULL); + this->m_mutex = mutex; + return; + } + + InitializeCriticalSection(&this->m_criticalSection); + this->m_mutex = NULL; +} + +MxCriticalSection::~MxCriticalSection() +{ + if (this->m_mutex != NULL) + { + CloseHandle(this->m_mutex); + return; + } + + DeleteCriticalSection(&this->m_criticalSection); +} + +void MxCriticalSection::SetDoMutex() +{ + g_useMutex = 1; +} + +void MxCriticalSection::Enter() +{ + DWORD result; + FILE *file; + + if (this->m_mutex != NULL) + { + result = WaitForSingleObject(this->m_mutex, 5000); + if (result == WAIT_FAILED) + { + file = fopen("C:\\DEADLOCK.TXT", "a"); + if (file != NULL) + { + fprintf(file, "mutex timeout occurred!\n"); + fclose(file); + } + + abort(); + } + } + else + { + EnterCriticalSection(&this->m_criticalSection); + } +} + +void MxCriticalSection::Leave() +{ + if (this->m_mutex != NULL) + { + ReleaseMutex(this->m_mutex); + return; + } + + LeaveCriticalSection(&this->m_criticalSection); +} \ No newline at end of file diff --git a/LEGO1/mxcriticalsection.h b/LEGO1/mxcriticalsection.h index 3e9e411c..4c8d43d0 100644 --- a/LEGO1/mxcriticalsection.h +++ b/LEGO1/mxcriticalsection.h @@ -1,12 +1,20 @@ -#ifndef MXCRITICALSECTION_H -#define MXCRITICALSECTION_H - -class MxCriticalSection -{ -public: - __declspec(dllexport) MxCriticalSection(); - __declspec(dllexport) ~MxCriticalSection(); - __declspec(dllexport) static void SetDoMutex(); -}; - -#endif // MXCRITICALSECTION_H +#ifndef MXCRITICALSECTION_H +#define MXCRITICALSECTION_H + +#include + +class MxCriticalSection +{ +public: + __declspec(dllexport) MxCriticalSection(); + __declspec(dllexport) ~MxCriticalSection(); + __declspec(dllexport) static void SetDoMutex(); + void Enter(); + void Leave(); + +private: + CRITICAL_SECTION m_criticalSection; + HANDLE m_mutex; +}; + +#endif // MXCRITICALSECTION_H diff --git a/isle.mak b/isle.mak index d98109dc..0dd74a84 100644 --- a/isle.mak +++ b/isle.mak @@ -57,6 +57,7 @@ CLEAN : -@erase "$(INTDIR)\dllmain.obj" -@erase "$(INTDIR)\legoomni.obj" -@erase "$(INTDIR)\mxcore.obj" + -@erase "$(INTDIR)\mxcriticalsection.obj" -@erase ".\Release\LEGO1.DLL" -@erase ".\Release\LEGO1.EXP" -@erase ".\Release\LEGO1.LIB" @@ -116,7 +117,8 @@ LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\ LINK32_OBJS= \ "$(INTDIR)\dllmain.obj" \ "$(INTDIR)\legoomni.obj" \ - "$(INTDIR)\mxcore.obj" + "$(INTDIR)\mxcore.obj" \ + "$(INTDIR)\mxcriticalsection.obj" ".\Release\LEGO1.DLL" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS) $(LINK32) @<< @@ -144,6 +146,7 @@ CLEAN : -@erase "$(INTDIR)\dllmain.obj" -@erase "$(INTDIR)\legoomni.obj" -@erase "$(INTDIR)\mxcore.obj" + -@erase "$(INTDIR)\mxcriticalsection.obj" -@erase "$(INTDIR)\vc40.idb" -@erase "$(INTDIR)\vc40.pdb" -@erase "$(OUTDIR)\LEGO1.exp" @@ -207,7 +210,8 @@ LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\ LINK32_OBJS= \ "$(INTDIR)\dllmain.obj" \ "$(INTDIR)\legoomni.obj" \ - "$(INTDIR)\mxcore.obj" + "$(INTDIR)\mxcore.obj" \ + "$(INTDIR)\mxcriticalsection.obj" ".\Debug\LEGO1.DLL" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS) $(LINK32) @<< @@ -477,6 +481,19 @@ DEP_CPP_LEGOO=\ $(CPP) $(CPP_PROJ) $(SOURCE) +# End Source File +################################################################################ +# Begin Source File + +SOURCE=.\LEGO1\mxcriticalsection.cpp +DEP_CPP_MXCRI=\ + ".\LEGO1\mxcriticalsection.h"\ + + +"$(INTDIR)\mxcriticalsection.obj" : $(SOURCE) $(DEP_CPP_MXCRI) "$(INTDIR)" + $(CPP) $(CPP_PROJ) $(SOURCE) + + # End Source File # End Target ################################################################################ diff --git a/isle.mdp b/isle.mdp index 61c43766..9b671ec0 100644 Binary files a/isle.mdp and b/isle.mdp differ