afxcrit.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10. #include "stdafx.h"
  11. #ifdef AFX_INIT_SEG
  12. #pragma code_seg(AFX_INIT_SEG)
  13. #endif
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char THIS_FILE[] = __FILE__;
  17. #endif
  18. #pragma warning(disable: 4706) // assignment within conditional
  19. /////////////////////////////////////////////////////////////////////////////
  20. // global thread protection
  21. #ifdef _MT
  22. AFX_STATIC_DATA BOOL _afxCriticalInit = 0; // set _afxGlobalLock, _afxTempLock init
  23. AFX_STATIC_DATA BOOL _afxCriticalWin32s = 0;
  24. // _afxResourceLock and _afxLockInit are used to lock each MFC global resource
  25. AFX_STATIC_DATA CRITICAL_SECTION _afxResourceLock[CRIT_MAX] = { { 0 } };
  26. AFX_STATIC_DATA CRITICAL_SECTION _afxLockInitLock = { 0 };
  27. AFX_STATIC_DATA BOOL _afxLockInit[CRIT_MAX] = { 0 };
  28. #ifdef _DEBUG
  29. AFX_STATIC_DATA BOOL _afxResourceLocked[CRIT_MAX] = { 0 };
  30. #endif
  31. BOOL AFXAPI AfxCriticalInit()
  32. {
  33. // Note: this must be initialized with only one thread running
  34. if (!_afxCriticalInit)
  35. {
  36. // now we are about to be initialized
  37. VERIFY(++_afxCriticalInit);
  38. // determine if running on Win32s (under Windows 3.x)
  39. DWORD dwVersion = GetVersion();
  40. _afxCriticalWin32s = ((BYTE)dwVersion) < 4 && (dwVersion & 0x80000000);
  41. // only initialize MT related stuff if on MT capable systems
  42. if (!_afxCriticalWin32s)
  43. InitializeCriticalSection(&_afxLockInitLock);
  44. }
  45. return _afxCriticalInit;
  46. }
  47. void AFXAPI AfxCriticalTerm()
  48. {
  49. if (_afxCriticalInit)
  50. {
  51. VERIFY(!--_afxCriticalInit);
  52. if (!_afxCriticalWin32s)
  53. {
  54. // delete helper critical sections
  55. DeleteCriticalSection(&_afxLockInitLock);
  56. // delete specific resource critical sections
  57. for (int i = 0; i < CRIT_MAX; i++)
  58. {
  59. #ifdef _DEBUG
  60. ASSERT(!_afxResourceLocked[i]);
  61. #endif
  62. if (_afxLockInit[i])
  63. {
  64. DeleteCriticalSection(&_afxResourceLock[i]);
  65. VERIFY(!--_afxLockInit[i]);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. void AFXAPI AfxLockGlobals(int nLockType)
  72. {
  73. ASSERT((UINT)nLockType < CRIT_MAX);
  74. // intialize global state, if necessary
  75. if (!_afxCriticalInit)
  76. {
  77. AfxCriticalInit();
  78. ASSERT(_afxCriticalInit);
  79. }
  80. // nothing necessary on Win32s (no multiple threads)
  81. if (_afxCriticalWin32s)
  82. return;
  83. // initialize specific resource if necessary
  84. if (!_afxLockInit[nLockType])
  85. {
  86. EnterCriticalSection(&_afxLockInitLock);
  87. if (!_afxLockInit[nLockType])
  88. {
  89. InitializeCriticalSection(&_afxResourceLock[nLockType]);
  90. VERIFY(++_afxLockInit[nLockType]);
  91. }
  92. LeaveCriticalSection(&_afxLockInitLock);
  93. }
  94. // lock specific resource
  95. EnterCriticalSection(&_afxResourceLock[nLockType]);
  96. #ifdef _DEBUG
  97. ASSERT(++_afxResourceLocked[nLockType] > 0);
  98. #endif
  99. }
  100. void AFXAPI AfxUnlockGlobals(int nLockType)
  101. {
  102. ASSERT(_afxCriticalInit);
  103. ASSERT((UINT)nLockType < CRIT_MAX);
  104. // nothing necessary on Win32s (no multiple threads)
  105. if (_afxCriticalWin32s)
  106. return;
  107. // unlock specific resource
  108. ASSERT(_afxLockInit[nLockType]);
  109. #ifdef _DEBUG
  110. ASSERT(--_afxResourceLocked[nLockType] >= 0);
  111. #endif
  112. LeaveCriticalSection(&_afxResourceLock[nLockType]);
  113. }
  114. #endif
  115. /////////////////////////////////////////////////////////////////////////////