[Project64] Move critical section code to cpp file and remove headers included

This commit is contained in:
zilmar 2015-10-25 20:10:28 +11:00
parent 647ce6ae78
commit 5012979377
6 changed files with 26 additions and 82 deletions

View File

@ -125,6 +125,10 @@
Name="Source Files" Name="Source Files"
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
> >
<File
RelativePath=".\CriticalSection.cpp"
>
</File>
<File <File
RelativePath="File Class.cpp" RelativePath="File Class.cpp"
> >

View File

@ -32,6 +32,7 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ItemGroup> <ItemGroup>
<ClCompile Include="CriticalSection.cpp" />
<ClCompile Include="File Class.cpp" /> <ClCompile Include="File Class.cpp" />
<ClCompile Include="Ini File Class.cpp" /> <ClCompile Include="Ini File Class.cpp" />
<ClCompile Include="Log Class.cpp" /> <ClCompile Include="Log Class.cpp" />
@ -55,6 +56,7 @@
<ClInclude Include="Smart Pointer.h" /> <ClInclude Include="Smart Pointer.h" />
<ClInclude Include="std string.h" /> <ClInclude Include="std string.h" />
<ClInclude Include="stdafx.h" /> <ClInclude Include="stdafx.h" />
<ClInclude Include="stdtypes.h" />
<ClInclude Include="Trace.h" /> <ClInclude Include="Trace.h" />
<ClInclude Include="TraceDefs.h" /> <ClInclude Include="TraceDefs.h" />
</ItemGroup> </ItemGroup>

View File

@ -38,6 +38,9 @@
<ClCompile Include="Trace.cpp"> <ClCompile Include="Trace.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="CriticalSection.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="CriticalSection.h"> <ClInclude Include="CriticalSection.h">
@ -76,5 +79,8 @@
<ClInclude Include="TraceDefs.h"> <ClInclude Include="TraceDefs.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="stdtypes.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,104 +1,33 @@
/**
* @file
*/
#pragma once #pragma once
#include <windows.h>
#include <exception>
#include <string>
/**
* Encapsulates a Win32 critical section.
* Provides control over creation and clean destruction of native win32 critical sections. Also
* allows the object to be used directly in place of a CRITICAL_SECTION handle via the void* overload
*
* @author Peter Hancock
*
*/
class CriticalSection class CriticalSection
{ {
public: public:
/** CriticalSection();
* Create the critical section. ~CriticalSection(void);
*/
CriticalSection() void enter(void);
{ void leave(void);
::InitializeCriticalSection(&cs);
}
/**
* Cleans up the critical section.
*/
~CriticalSection(void)
{
::DeleteCriticalSection(&cs);
}
/**
* Enters a critical section of code.
* Prevents other threads from accessing the section between the enter and leave sections simultaneously.
* @note It is good practice to try and keep the critical section code as little as possible, so that
* other threads are not locked waiting for it.
*/
void enter(void)
{
::EnterCriticalSection(&cs);
}
/**
* Leaves the critical section.
* Allows threads access to the critical code section again.
* @warning Note that an exception occurring with a critical section may not result in the expected leave being
* called. To ensure that your critical section is exception safe, ensure that you wrap the critical
* section in a try catch, and the catch calls the leave method.
*/
void leave(void)
{
::LeaveCriticalSection(&cs);
}
/**
* Provides conversion to the native WIN32 CRITICAL_SECTION object
* Allows the client to use the CriticalSection object as a native handle also.
* @return CRITICAL_SECTION handle
*/
operator CRITICAL_SECTION&()
{
return cs;
}
private: private:
CriticalSection(const CriticalSection&); // Disable copy constructor CriticalSection(const CriticalSection&); // Disable copy constructor
CriticalSection& operator=(const CriticalSection&); // Disable assignment CriticalSection& operator=(const CriticalSection&); // Disable assignment
CRITICAL_SECTION cs; void * m_cs;
}; };
/**
* CGuard class provides exception safety for critical sections
* A helper class that enters a critical section on construction, and leaves
* the critical section on destruction.
*
* @code
int i;
{
CGuard loopGuard(moduleCS); // enters the critical section
for(int i=0 ; i < 10 ; i++)
{
// Do stuff here // If an exception is thrown here, loopGuard goes out of scope - calling leave()
}
} // Guard goes out of scope here, destructor calls leave()
* @endcode
*
* @author Peter Hancock
*
*/
class CGuard class CGuard
{ {
public: public:
CGuard(CriticalSection& sectionName) : cs(sectionName) CGuard(CriticalSection& sectionName) : m_cs(sectionName)
{ {
cs.enter(); m_cs.enter();
} }
~CGuard() ~CGuard()
{ {
cs.leave(); m_cs.leave();
} }
private: private:
CriticalSection& cs; CriticalSection& m_cs;
CGuard(const CGuard& copy); CGuard(const CGuard& copy);
CGuard &operator=(const CGuard& rhs); CGuard &operator=(const CGuard& rhs);
}; };

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <windows.h> #include <windows.h>
#include <exception>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>

View File

@ -15,6 +15,8 @@
#include <common/MemTest.h> #include <common/MemTest.h>
#include <common/CriticalSection.h> #include <common/CriticalSection.h>
#include <windows.h>
#include <exception>
#include <shellapi.h> #include <shellapi.h>
#include "Multilanguage.h" #include "Multilanguage.h"
#include "User Interface.h" #include "User Interface.h"