home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n08 / oleq0895.exe / SSDA.CPP < prev    next >
C/C++ Source or Header  |  1995-08-01  |  2KB  |  112 lines

  1. #include "stdafx.h"
  2. #include "SSDA.h"
  3.  
  4. SharedSortedDWORDArray::SharedSortedDWORDArray(LPCTSTR szName)
  5. :    m_hsection(0),
  6.     m_hmutex(0),
  7.     m_pdwCount(0),
  8.     m_pdwIDs(0)
  9. {
  10.   TCHAR szMutexName[64];
  11.   TCHAR szSectionName[64];
  12.  
  13. // synthesize a mutex and section name
  14.   wsprintf(szMutexName, __TEXT("%s_Mtx"), szName);
  15.   wsprintf(szSectionName, __TEXT("%s_Scn"), szName);
  16.  
  17. // create/open the Mutex
  18.     m_hmutex = CreateMutex(0, TRUE, szMutexName);
  19.     BOOL bFirstApp = GetLastError() != ERROR_ALREADY_EXISTS;
  20.     
  21. // create/open the section object
  22.     m_hsection = CreateFileMapping(HANDLE(0xFFFFFFFF),
  23.                                                                0, PAGE_READWRITE,
  24.                                                                0, sizeof(DWORD) * (MAX_DWORDS + 1),
  25.                                                                  szSectionName);
  26.  
  27.  
  28. // the first dword in the section will contain the array size
  29.     m_pdwCount = (DWORD*)MapViewOfFile(m_hsection,    FILE_MAP_ALL_ACCESS,
  30.                                                                          0, 0, 0);
  31.  
  32. // dwords 2 - N will contain the array
  33.     m_pdwIDs = m_pdwCount + 1;                                                                            
  34.  
  35. // first thread inits the count to zero
  36.     if (bFirstApp)
  37.         {
  38.             *m_pdwCount = 0;
  39.             ReleaseMutex(m_hmutex);
  40.         }
  41. }
  42.  
  43. SharedSortedDWORDArray::~SharedSortedDWORDArray(void)
  44. {
  45. // release all objects alloced in constructor
  46.     if (m_pdwCount)
  47.         UnmapViewOfFile(m_pdwCount);
  48.     if (m_hsection)
  49.         CloseHandle(m_hsection);
  50.     if (m_hmutex)
  51.         CloseHandle(m_hmutex);
  52. }
  53.  
  54. BOOL 
  55. SharedSortedDWORDArray::Insert(DWORD id)
  56. {
  57.     BOOL result = FALSE;
  58. // lock the array
  59.     WaitForSingleObject(m_hmutex, INFINITE);
  60.  
  61. // remove id to avoid duplicates
  62.     Remove(id);
  63.  
  64. // insert at end of array
  65.     if (*m_pdwCount < MAX_DWORDS) 
  66.         {
  67.             m_pdwIDs[*m_pdwCount] = id;
  68.             (*m_pdwCount)++;    
  69.             result = TRUE;
  70.         }
  71.  
  72. // unlock the array
  73.     ReleaseMutex(m_hmutex);      
  74.     return result;
  75. }
  76.  
  77. void 
  78. SharedSortedDWORDArray::Remove(DWORD id)
  79. {
  80. // lock the array
  81.     WaitForSingleObject(m_hmutex, INFINITE);
  82.  
  83. // search array for id and remove if found
  84.     for (DWORD i = 0; i < *m_pdwCount; i++)
  85.         if (m_pdwIDs[i] == id)
  86.             {
  87.                 MoveMemory(m_pdwIDs + i, m_pdwIDs + i + 1, sizeof(DWORD) * (*m_pdwCount - 1 - i));
  88.                 (*m_pdwCount)--;
  89.                 break;
  90.             }
  91.  
  92. // unlock the array
  93.     ReleaseMutex(m_hmutex);      
  94. }
  95.  
  96. BOOL 
  97. SharedSortedDWORDArray::IsTop(DWORD id)
  98. {
  99. // lock the array
  100.     WaitForSingleObject(m_hmutex, INFINITE);
  101.     BOOL result = FALSE;
  102.  
  103. // test last element against id
  104.     if (*m_pdwCount)
  105.         result = (id == m_pdwIDs[(*m_pdwCount) - 1]);
  106.  
  107. // unlock array
  108.     ReleaseMutex(m_hmutex);      
  109.     return result;
  110. }
  111.  
  112.