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.

117 lines
2.9 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. fusioninitializecriticalsection.cpp
  5. Abstract:
  6. non throwing replacements for InitializeCriticalSection and InitializeCriticalAndSpinCount
  7. Author:
  8. Jay Krell (JayKrell) November 2001
  9. Revision History:
  10. --*/
  11. #include "stdinc.h"
  12. #include "fusioninitializecriticalsection.h"
  13. #include "sxsexceptionhandling.h"
  14. #define FUSIONP_INITIALIZE_CRITICAL_SECTION_AND_SPIN_COUNT (0x00000001)
  15. BOOL
  16. FusionpInitializeCriticalSectionCommon(
  17. ULONG Flags,
  18. LPCRITICAL_SECTION CriticalSection,
  19. DWORD SpinCount
  20. )
  21. {
  22. BOOL Result = FALSE;
  23. #if defined(FUSION_WIN2000)
  24. typedef BOOL (WINAPI * PFN)(LPCRITICAL_SECTION CriticalSection, DWORD SpinCount);
  25. static PFN s_pfn;
  26. static BOOL s_initialized;
  27. #endif
  28. __try
  29. {
  30. if ((Flags & FUSIONP_INITIALIZE_CRITICAL_SECTION_AND_SPIN_COUNT) != 0)
  31. {
  32. #if defined(FUSION_WIN)
  33. Result = ::InitializeCriticalSectionAndSpinCount(CriticalSection, SpinCount);
  34. #elif defined(FUSION_WIN2000)
  35. if (!s_initialized)
  36. {
  37. HMODULE Kernel32Dll = ::LoadLibraryW(L"Kernel32.dll");
  38. if (Kernel32Dll == NULL && ::GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
  39. {
  40. Kernel32Dll = ::LoadLibraryA("Kernel32.dll");
  41. }
  42. if (Kernel32Dll != NULL)
  43. {
  44. s_pfn = reinterpret_cast<PFN>(GetProcAddress(Kernel32Dll, "InitializeCriticalSectionAndSpinCount"));
  45. }
  46. s_initialized = TRUE;
  47. }
  48. if (s_pfn != NULL)
  49. {
  50. Result = (*s_pfn)(CriticalSection, SpinCount);
  51. }
  52. else
  53. {
  54. ::InitializeCriticalSection(CriticalSection);
  55. Result = TRUE;
  56. }
  57. #else
  58. #error
  59. #endif
  60. }
  61. else
  62. {
  63. ::InitializeCriticalSection(CriticalSection);
  64. Result = TRUE;
  65. }
  66. }
  67. __except(SXSP_EXCEPTION_FILTER())
  68. {
  69. SXS_REPORT_SEH_EXCEPTION("", false);
  70. ::FusionpSetLastWin32Error(
  71. #if FUSION_STATIC_NTDLL
  72. ::RtlNtStatusToDosErrorNoTeb(GetExceptionCode())
  73. #else
  74. ERROR_OUTOFMEMORY
  75. #endif
  76. );
  77. }
  78. //Exit:
  79. return Result;
  80. }
  81. BOOL
  82. FusionpInitializeCriticalSectionAndSpinCount(
  83. LPCRITICAL_SECTION CriticalSection,
  84. DWORD SpinCount
  85. )
  86. {
  87. return FusionpInitializeCriticalSectionCommon(
  88. FUSIONP_INITIALIZE_CRITICAL_SECTION_AND_SPIN_COUNT,
  89. CriticalSection,
  90. SpinCount
  91. );
  92. }
  93. BOOL
  94. FusionpInitializeCriticalSection(
  95. LPCRITICAL_SECTION CriticalSection
  96. )
  97. {
  98. return FusionpInitializeCriticalSectionCommon(
  99. 0,
  100. CriticalSection,
  101. 0
  102. );
  103. }