home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectPlay / Maze / MazeCommon / SyncObjects.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  5.5 KB  |  205 lines

  1. //----------------------------------------------------------------------------
  2. // File: syncobjects.h
  3. //
  4. // Desc: see main.cpp
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #ifndef _SYNC_OBJECTS_H
  9. #define _SYNC_OBJECTS_H
  10.  
  11.  
  12. #include <assert.h>
  13.  
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Name: 
  17. // Desc: Simple wrapper for critical section
  18. //-----------------------------------------------------------------------------
  19. class CCriticalSection
  20. {
  21. public:
  22.     CCriticalSection( DWORD spincount = 2000 )
  23.     {
  24.         InitializeCriticalSection( &m_CritSec );
  25.     };
  26.  
  27.     ~CCriticalSection()
  28.     {
  29.         DeleteCriticalSection( &m_CritSec );
  30.     };
  31.  
  32.     void    Enter()
  33.     {
  34.         EnterCriticalSection( &m_CritSec );
  35.     };
  36.  
  37.     void    Leave()
  38.     {
  39.         LeaveCriticalSection( &m_CritSec );
  40.     };
  41.  
  42. private:
  43.     CRITICAL_SECTION    m_CritSec;
  44. };
  45.  
  46.  
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Name: 
  51. // Desc: Array of critical section objects. The methods allow locking single 
  52. //       elements, rectangular regions, or a combination. Using these methods 
  53. //       ensures the cells are locked/unlocked in a consistent order
  54. //       which prevents deadlocks.
  55. //-----------------------------------------------------------------------------
  56. template< DWORD width , DWORD height > class    CLockArray
  57. {
  58. public:
  59.     #define CS_RESOLUTION 4
  60.  
  61.     // Lock/Unlock a single cell
  62.     void    LockCell( DWORD x , DWORD y )
  63.     {
  64.         x /= CS_RESOLUTION;
  65.         y /= CS_RESOLUTION;
  66.  
  67.         assert( x<width && y<height );
  68.  
  69.         m_Grid[y][x].Enter();
  70.     };
  71.  
  72.     void    UnlockCell( DWORD x , DWORD y )
  73.     {
  74.         x /= CS_RESOLUTION;
  75.         y /= CS_RESOLUTION;
  76.  
  77.         assert( x<width && y<height );
  78.  
  79.         m_Grid[y][x].Leave();
  80.     };
  81.  
  82.     // Lock/Unlock a rectangular range of cells
  83.     void    LockRange( DWORD x1 , DWORD y1 , DWORD x2 , DWORD y2 )
  84.     {
  85.         x1 /= CS_RESOLUTION;
  86.         y1 /= CS_RESOLUTION;
  87.         x2 /= CS_RESOLUTION;
  88.         y2 /= CS_RESOLUTION;
  89.  
  90.         if ( x1 > x2 ) { DWORD t = x1; x1 = x2; x2 = t; }; // x1 == min
  91.         if ( y1 > y2 ) { DWORD t = y1; y1 = y2; y2 = t; }; // y1 == min
  92.  
  93.         assert( x1 <= x2 && y1 <= y2 );
  94.         assert( x1<width && y1<height );
  95.         assert( x2<width && y2<height );
  96.  
  97.         // Lock from xmin,ymin to xmax,ymax (from xmin,y to xmax,y first) 
  98.         for ( INT y = y1 ; y <= (INT) y2 ; y++ )
  99.             for ( INT x = x1 ; x <= (INT) x2 ; x++ )
  100.                 m_Grid[y][x].Enter();
  101.     };
  102.  
  103.     void    UnlockRange( DWORD x1 , DWORD y1 , DWORD x2 , DWORD y2 )
  104.     {
  105.         x1 /= CS_RESOLUTION;
  106.         y1 /= CS_RESOLUTION;
  107.         x2 /= CS_RESOLUTION;
  108.         y2 /= CS_RESOLUTION;
  109.  
  110.         if ( x1 > x2 ) { DWORD t = x1; x1 = x2; x2 = t; }; // x1 == min
  111.         if ( y1 > y2 ) { DWORD t = y1; y1 = y2; y2 = t; }; // y1 == min
  112.  
  113.         assert( x1 <= x2 && y1 <= y2 );
  114.         assert( x1<width && y1<height );
  115.         assert( x2<width && y2<height );
  116.  
  117.         // Unlock from xmax,ymax to xmin,ymin 
  118.         for ( INT y = y2 ; y >= (INT) y1 ; y-- )
  119.             for ( INT x = x2 ; x >= (INT) x1 ; x-- )
  120.                 m_Grid[y][x].Leave();
  121.     };
  122.  
  123.     void LockCellPair( DWORD x1, DWORD y1, DWORD x2, DWORD y2 )
  124.     {
  125.         x1 /= CS_RESOLUTION;
  126.         y1 /= CS_RESOLUTION;
  127.         x2 /= CS_RESOLUTION;
  128.         y2 /= CS_RESOLUTION;
  129.  
  130.         assert( x1<width && y1<height );
  131.         assert( x2<width && y2<height );
  132.  
  133.         if( x1 == x2 && y1 == y2 )
  134.         {
  135.             LockCell( x1, y1 );
  136.             return;
  137.         }
  138.  
  139.         if( (y1<y2) || ((y1==y2)&&(x1<=x2)) )
  140.         {
  141.             assert( ((y1<y2)) ||             // y1 < y2 case
  142.                     ((y1==y2)&&(x1<=x2)) );  // y1 == y2 case
  143.  
  144.             // Lock from xmin,ymin to xmax,ymax (from xmin,y to xmax,y first)
  145.             LockCell(x1,y1);
  146.             LockCell(x2,y2);
  147.         }
  148.         else
  149.         {
  150.             assert( ((y1>=y2)) ||            // y1 < y2 case
  151.                     ((y1==y2)&&(x1>x2)) );   // y1 == y2 case
  152.  
  153.             // Lock from xmin,ymin to xmax,ymax (from xmin,y to xmax,y first) 
  154.             LockCell(x2,y2);
  155.             LockCell(x1,y1);
  156.         }
  157.     }
  158.  
  159.  
  160.     void UnlockCellPair( DWORD x1, DWORD y1, DWORD x2, DWORD y2 )
  161.     {
  162.         x1 /= CS_RESOLUTION;
  163.         y1 /= CS_RESOLUTION;
  164.         x2 /= CS_RESOLUTION;
  165.         y2 /= CS_RESOLUTION;
  166.  
  167.         assert( x1<width && y1<height );
  168.         assert( x2<width && y2<height );
  169.  
  170.         if( x1 == x2 && y1 == y2 )
  171.         {
  172.             UnlockCell( x1, y1 );
  173.             return;
  174.         }
  175.  
  176.         if( (y1<y2) || ((y1==y2)&&(x1<=x2)) )
  177.         {
  178.             assert( ((y1<y2)) ||             // y1 < y2 case
  179.                     ((y1==y2)&&(x1<=x2)) );  // y1 == y2 case
  180.  
  181.             // Unlock from xmax,ymax to xmin,ymin (from xmax,y to xmin,y first)  
  182.             UnlockCell(x2,y2);
  183.             UnlockCell(x1,y1);
  184.         }
  185.         else
  186.         {
  187.             assert( ((y1>=y2)) ||            // y1 < y2 case
  188.                     ((y1==y2)&&(x1>x2)) );   // y1 == y2 case
  189.  
  190.             // Unlock from xmax,ymax to xmin,ymin (from xmax,y to xmin,y first)  
  191.             UnlockCell(x1,y1);
  192.             UnlockCell(x2,y2);
  193.         }
  194.     }
  195.  
  196.  
  197. private:
  198.     CCriticalSection    m_Grid[height][width];
  199. };
  200.  
  201.  
  202.  
  203.  
  204. #endif
  205.