Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.2 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. syncobj.hxx
  5. Abstract:
  6. Contains CCritSec helper class for working with critical sections.
  7. For now, there are no cross-process locks needed in winhttp, so this
  8. file only contains a wrapper for critical sections. This object
  9. will provide exception handling and checking to help protect
  10. against low-memory conditions that can generate exceptions.
  11. Contents:
  12. CCritSec
  13. Revision History:
  14. Created 21-Mar-2001
  15. --*/
  16. #ifndef __SYNCOBJ_HXX__
  17. #define __SYNCOBJ_HXX__ 1
  18. struct CCritSec
  19. {
  20. private:
  21. CRITICAL_SECTION _cs;
  22. BOOL _fInitialized;
  23. public:
  24. CCritSec() : _fInitialized(FALSE)
  25. {
  26. // Initialized the critical section on demand
  27. // via the Init() method.
  28. }
  29. ~CCritSec()
  30. {
  31. FreeLock();
  32. }
  33. BOOL Init()
  34. {
  35. __try
  36. {
  37. // protect against STATUS_NO_MEMORY exceptions
  38. // in low memory (paged pool) conditions.
  39. InitializeCriticalSection(&_cs);
  40. _fInitialized = TRUE;
  41. return TRUE;
  42. }
  43. __except(EXCEPTION_EXECUTE_HANDLER)
  44. {
  45. }
  46. return FALSE;
  47. }
  48. BOOL Lock()
  49. {
  50. if (!_fInitialized)
  51. {
  52. return FALSE;
  53. }
  54. __try
  55. {
  56. // Word on the street is that EnterCriticalSection
  57. // can also generate a STATUS_NO_MEMORY exception,
  58. // at least on NT, if the system is out of non-paged
  59. // pool memory.
  60. EnterCriticalSection(&_cs);
  61. return TRUE;
  62. }
  63. __except(EXCEPTION_EXECUTE_HANDLER)
  64. {
  65. }
  66. return FALSE;
  67. }
  68. void Unlock()
  69. {
  70. if (_fInitialized)
  71. {
  72. LeaveCriticalSection(&_cs);
  73. }
  74. }
  75. void FreeLock()
  76. {
  77. if (_fInitialized)
  78. {
  79. DeleteCriticalSection(&_cs);
  80. _fInitialized = FALSE;
  81. }
  82. }
  83. BOOL IsInitialized()
  84. {
  85. return _fInitialized;
  86. }
  87. };
  88. #endif // __SYNCOBJ_HXX__