home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / afxcrit.cpp < prev    next >
C/C++ Source or Header  |  1998-06-16  |  4KB  |  140 lines

  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.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #pragma warning(disable: 4706) // assignment within conditional
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // global thread protection
  26.  
  27. #ifdef _MT
  28.  
  29. AFX_STATIC_DATA BOOL _afxCriticalInit = 0;   // set _afxGlobalLock, _afxTempLock init
  30. AFX_STATIC_DATA BOOL _afxCriticalWin32s = 0;
  31.  
  32. // _afxResourceLock and _afxLockInit are used to lock each MFC global resource
  33. AFX_STATIC_DATA CRITICAL_SECTION _afxResourceLock[CRIT_MAX] = { { 0 } };
  34. AFX_STATIC_DATA CRITICAL_SECTION _afxLockInitLock = { 0 };
  35. AFX_STATIC_DATA BOOL _afxLockInit[CRIT_MAX] = { 0 };
  36. #ifdef _DEBUG
  37. AFX_STATIC_DATA BOOL _afxResourceLocked[CRIT_MAX] = { 0 };
  38. #endif
  39.  
  40. BOOL AFXAPI AfxCriticalInit()
  41. {
  42.     // Note: this must be initialized with only one thread running
  43.     if (!_afxCriticalInit)
  44.     {
  45.         // now we are about to be initialized
  46.         VERIFY(++_afxCriticalInit);
  47.  
  48.         // determine if running on Win32s (under Windows 3.x)
  49.         DWORD dwVersion = GetVersion();
  50.         _afxCriticalWin32s = ((BYTE)dwVersion) < 4 && (dwVersion & 0x80000000);
  51.  
  52.         // only initialize MT related stuff if on MT capable systems
  53.         if (!_afxCriticalWin32s)
  54.             InitializeCriticalSection(&_afxLockInitLock);
  55.     }
  56.     return _afxCriticalInit;
  57. }
  58.  
  59. void AFXAPI AfxCriticalTerm()
  60. {
  61.     if (_afxCriticalInit)
  62.     {
  63.         VERIFY(!--_afxCriticalInit);
  64.  
  65.         if (!_afxCriticalWin32s)
  66.         {
  67.             // delete helper critical sections
  68.             DeleteCriticalSection(&_afxLockInitLock);
  69.  
  70.             // delete specific resource critical sections
  71.             for (int i = 0; i < CRIT_MAX; i++)
  72.             {
  73. #ifdef _DEBUG
  74.                 ASSERT(!_afxResourceLocked[i]);
  75. #endif
  76.                 if (_afxLockInit[i])
  77.                 {
  78.                     DeleteCriticalSection(&_afxResourceLock[i]);
  79.                     VERIFY(!--_afxLockInit[i]);
  80.                 }
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. void AFXAPI AfxLockGlobals(int nLockType)
  87. {
  88.     ASSERT((UINT)nLockType < CRIT_MAX);
  89.  
  90.     // intialize global state, if necessary
  91.     if (!_afxCriticalInit)
  92.     {
  93.         AfxCriticalInit();
  94.         ASSERT(_afxCriticalInit);
  95.     }
  96.  
  97.     // nothing necessary on Win32s (no multiple threads)
  98.     if (_afxCriticalWin32s)
  99.         return;
  100.  
  101.     // initialize specific resource if necessary
  102.     if (!_afxLockInit[nLockType])
  103.     {
  104.         EnterCriticalSection(&_afxLockInitLock);
  105.         if (!_afxLockInit[nLockType])
  106.         {
  107.             InitializeCriticalSection(&_afxResourceLock[nLockType]);
  108.             VERIFY(++_afxLockInit[nLockType]);
  109.         }
  110.         LeaveCriticalSection(&_afxLockInitLock);
  111.     }
  112.  
  113.     // lock specific resource
  114.     EnterCriticalSection(&_afxResourceLock[nLockType]);
  115. #ifdef _DEBUG
  116.     ASSERT(++_afxResourceLocked[nLockType] > 0);
  117. #endif
  118. }
  119.  
  120. void AFXAPI AfxUnlockGlobals(int nLockType)
  121. {
  122.     ASSERT(_afxCriticalInit);
  123.     ASSERT((UINT)nLockType < CRIT_MAX);
  124.  
  125.     // nothing necessary on Win32s (no multiple threads)
  126.     if (_afxCriticalWin32s)
  127.         return;
  128.  
  129.     // unlock specific resource
  130.     ASSERT(_afxLockInit[nLockType]);
  131. #ifdef _DEBUG
  132.     ASSERT(--_afxResourceLocked[nLockType] >= 0);
  133. #endif
  134.     LeaveCriticalSection(&_afxResourceLock[nLockType]);
  135. }
  136.  
  137. #endif
  138.  
  139. /////////////////////////////////////////////////////////////////////////////
  140.