[Project64] Move critical section code to cpp file and remove headers included
This commit is contained in:
parent
647ce6ae78
commit
5012979377
|
@ -125,6 +125,10 @@
|
|||
Name="Source Files"
|
||||
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\CriticalSection.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="File Class.cpp"
|
||||
>
|
||||
|
|
|
@ -32,6 +32,7 @@
|
|||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ItemGroup>
|
||||
<ClCompile Include="CriticalSection.cpp" />
|
||||
<ClCompile Include="File Class.cpp" />
|
||||
<ClCompile Include="Ini File Class.cpp" />
|
||||
<ClCompile Include="Log Class.cpp" />
|
||||
|
@ -55,6 +56,7 @@
|
|||
<ClInclude Include="Smart Pointer.h" />
|
||||
<ClInclude Include="std string.h" />
|
||||
<ClInclude Include="stdafx.h" />
|
||||
<ClInclude Include="stdtypes.h" />
|
||||
<ClInclude Include="Trace.h" />
|
||||
<ClInclude Include="TraceDefs.h" />
|
||||
</ItemGroup>
|
||||
|
|
|
@ -38,6 +38,9 @@
|
|||
<ClCompile Include="Trace.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="CriticalSection.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="CriticalSection.h">
|
||||
|
@ -76,5 +79,8 @@
|
|||
<ClInclude Include="TraceDefs.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="stdtypes.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -1,104 +1,33 @@
|
|||
/**
|
||||
* @file
|
||||
*/
|
||||
#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
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create the critical section.
|
||||
*/
|
||||
CriticalSection()
|
||||
{
|
||||
::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;
|
||||
}
|
||||
CriticalSection();
|
||||
~CriticalSection(void);
|
||||
|
||||
void enter(void);
|
||||
void leave(void);
|
||||
|
||||
private:
|
||||
CriticalSection(const CriticalSection&); // Disable copy constructor
|
||||
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
|
||||
{
|
||||
public:
|
||||
CGuard(CriticalSection& sectionName) : cs(sectionName)
|
||||
CGuard(CriticalSection& sectionName) : m_cs(sectionName)
|
||||
{
|
||||
cs.enter();
|
||||
m_cs.enter();
|
||||
}
|
||||
~CGuard()
|
||||
{
|
||||
cs.leave();
|
||||
m_cs.leave();
|
||||
}
|
||||
private:
|
||||
CriticalSection& cs;
|
||||
CriticalSection& m_cs;
|
||||
CGuard(const CGuard& copy);
|
||||
CGuard &operator=(const CGuard& rhs);
|
||||
};
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <map>
|
||||
#include <windows.h>
|
||||
#include <exception>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@
|
|||
|
||||
#include <common/MemTest.h>
|
||||
#include <common/CriticalSection.h>
|
||||
#include <windows.h>
|
||||
#include <exception>
|
||||
#include <shellapi.h>
|
||||
#include "Multilanguage.h"
|
||||
#include "User Interface.h"
|
||||
|
|
Loading…
Reference in New Issue