home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / AFXCRIT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  3.4 KB  |  134 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 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. static BOOL _afxCriticalInit;   // set _afxGlobalLock, _afxTempLock init
  30. static BOOL _afxCriticalWin32s;
  31.  
  32. // _afxResourceLock and _afxLockInit are used to lock each MFC global resource
  33. static CRITICAL_SECTION _afxResourceLock[CRIT_MAX];
  34. static CRITICAL_SECTION _afxLockInitLock;
  35. static BOOL _afxLockInit[CRIT_MAX];
  36. #ifdef _DEBUG
  37. static BOOL _afxResourceLocked[CRIT_MAX];
  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.                 ASSERT(!_afxResourceLocked[i]);
  74.                 if (_afxLockInit[i])
  75.                 {
  76.                     DeleteCriticalSection(&_afxResourceLock[i]);
  77.                     VERIFY(!--_afxLockInit[i]);
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. void AFXAPI AfxLockGlobals(int nLockType)
  85. {
  86.     ASSERT((UINT)nLockType < CRIT_MAX);
  87.  
  88.     // intialize global state, if necessary
  89.     if (!_afxCriticalInit)
  90.     {
  91.         AfxCriticalInit();
  92.         ASSERT(_afxCriticalInit);
  93.     }
  94.  
  95.     // nothing necessary on Win32s (no multiple threads)
  96.     if (_afxCriticalWin32s)
  97.         return;
  98.  
  99.     // initialize specific resource if necessary
  100.     if (!_afxLockInit[nLockType])
  101.     {
  102.         EnterCriticalSection(&_afxLockInitLock);
  103.         if (!_afxLockInit[nLockType])
  104.         {
  105.             InitializeCriticalSection(&_afxResourceLock[nLockType]);
  106.             VERIFY(++_afxLockInit[nLockType]);
  107.         }
  108.         LeaveCriticalSection(&_afxLockInitLock);
  109.     }
  110.  
  111.     // lock specific resource
  112.     EnterCriticalSection(&_afxResourceLock[nLockType]);
  113.     ASSERT(++_afxResourceLocked[nLockType] > 0);
  114. }
  115.  
  116. void AFXAPI AfxUnlockGlobals(int nLockType)
  117. {
  118.     ASSERT(_afxCriticalInit);
  119.     ASSERT((UINT)nLockType < CRIT_MAX);
  120.  
  121.     // nothing necessary on Win32s (no multiple threads)
  122.     if (_afxCriticalWin32s)
  123.         return;
  124.  
  125.     // unlock specific resource
  126.     ASSERT(_afxLockInit[nLockType]);
  127.     ASSERT(--_afxResourceLocked[nLockType] >= 0);
  128.     LeaveCriticalSection(&_afxResourceLock[nLockType]);
  129. }
  130.  
  131. #endif
  132.  
  133. /////////////////////////////////////////////////////////////////////////////
  134.