CriticalSection.h
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:2k
源码类别:

模拟服务器

开发平台:

C/C++

  1. /********************************************************************
  2. created: 2003/02/13
  3. file base: CriticalSection
  4. file ext: h
  5. author: liupeng
  6. purpose: Header file for CRITICAL_SECTION class
  7. *********************************************************************/
  8. #ifndef __INCLUDE_CRITICALSECTION_H__
  9. #define __INCLUDE_CRITICALSECTION_H__
  10. #if defined (_MSC_VER) && (_MSC_VER >= 1020)
  11. #pragma once
  12. #endif
  13. #ifndef _WINDOWS_
  14. #define WIN32_LEAN_AND_MEAN
  15. #include <windows.h>
  16. #undef WIN32_LEAN_AND_MEAN
  17. #endif
  18. #include "Utils.h"
  19. /*
  20.  * namespace OnlineGameLib::Win32
  21.  */
  22. namespace OnlineGameLib {
  23. namespace Win32 {
  24. /*
  25.  * CCriticalSection
  26.  */
  27. class CCriticalSection 
  28. {
  29. public:
  30. class Owner
  31. {
  32. public:
  33. explicit Owner( CCriticalSection &crit );
  34. ~Owner();
  35. private:
  36. CCriticalSection &m_crit;
  37. /*
  38.  * No copies do not implement
  39.  */
  40. Owner( const Owner &rhs );
  41. Owner &operator=( const Owner &rhs );
  42. };
  43. CCriticalSection();
  44. ~CCriticalSection();
  45. #if ( _WIN32_WINNT >= 0x0400 )
  46. bool TryEnter();
  47. #endif
  48.   
  49.     void Enter();
  50.   
  51.     void Leave();
  52.   
  53. private:
  54. CRITICAL_SECTION m_crit;
  55. /*
  56.  * No copies do not implement
  57.  */
  58. CCriticalSection( const CCriticalSection &rhs );
  59. CCriticalSection &operator=( const CCriticalSection &rhs );
  60. };
  61. inline void CCriticalSection::Enter()
  62. {
  63. ::EnterCriticalSection( &m_crit );
  64. }
  65. inline void CCriticalSection::Leave()
  66. {
  67. ::LeaveCriticalSection( &m_crit );
  68. }
  69. inline CCriticalSection::CCriticalSection()
  70. {
  71. ::InitializeCriticalSection( &m_crit );
  72. }
  73.       
  74. inline CCriticalSection::~CCriticalSection()
  75. {
  76. ::DeleteCriticalSection( &m_crit );
  77. }
  78. #if ( _WIN32_WINNT >= 0x0400 )
  79. inline bool CCriticalSection::TryEnter()
  80. {
  81. return BOOL_to_bool( ::TryEnterCriticalSection( &m_crit ) );
  82. }
  83. #endif
  84. /*
  85.  * CCriticalSection::Owner
  86.  */
  87. inline CCriticalSection::Owner::Owner( CCriticalSection &crit )
  88. : m_crit( crit )
  89. {
  90. m_crit.Enter();
  91. }
  92. inline CCriticalSection::Owner::~Owner()
  93. {
  94. m_crit.Leave();
  95. }
  96. } // End of namespace Win32 
  97. } // End of namespace OnlineGameLib
  98. #endif //__INCLUDE_CRITICALSECTION_H__