home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Troubleshooting Netware Systems
/
CSTRIAL0196.BIN
/
attach
/
msj
/
v10n08
/
oleq0895.exe
/
SSDA.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-08-01
|
2KB
|
112 lines
#include "stdafx.h"
#include "SSDA.h"
SharedSortedDWORDArray::SharedSortedDWORDArray(LPCTSTR szName)
: m_hsection(0),
m_hmutex(0),
m_pdwCount(0),
m_pdwIDs(0)
{
TCHAR szMutexName[64];
TCHAR szSectionName[64];
// synthesize a mutex and section name
wsprintf(szMutexName, __TEXT("%s_Mtx"), szName);
wsprintf(szSectionName, __TEXT("%s_Scn"), szName);
// create/open the Mutex
m_hmutex = CreateMutex(0, TRUE, szMutexName);
BOOL bFirstApp = GetLastError() != ERROR_ALREADY_EXISTS;
// create/open the section object
m_hsection = CreateFileMapping(HANDLE(0xFFFFFFFF),
0, PAGE_READWRITE,
0, sizeof(DWORD) * (MAX_DWORDS + 1),
szSectionName);
// the first dword in the section will contain the array size
m_pdwCount = (DWORD*)MapViewOfFile(m_hsection, FILE_MAP_ALL_ACCESS,
0, 0, 0);
// dwords 2 - N will contain the array
m_pdwIDs = m_pdwCount + 1;
// first thread inits the count to zero
if (bFirstApp)
{
*m_pdwCount = 0;
ReleaseMutex(m_hmutex);
}
}
SharedSortedDWORDArray::~SharedSortedDWORDArray(void)
{
// release all objects alloced in constructor
if (m_pdwCount)
UnmapViewOfFile(m_pdwCount);
if (m_hsection)
CloseHandle(m_hsection);
if (m_hmutex)
CloseHandle(m_hmutex);
}
BOOL
SharedSortedDWORDArray::Insert(DWORD id)
{
BOOL result = FALSE;
// lock the array
WaitForSingleObject(m_hmutex, INFINITE);
// remove id to avoid duplicates
Remove(id);
// insert at end of array
if (*m_pdwCount < MAX_DWORDS)
{
m_pdwIDs[*m_pdwCount] = id;
(*m_pdwCount)++;
result = TRUE;
}
// unlock the array
ReleaseMutex(m_hmutex);
return result;
}
void
SharedSortedDWORDArray::Remove(DWORD id)
{
// lock the array
WaitForSingleObject(m_hmutex, INFINITE);
// search array for id and remove if found
for (DWORD i = 0; i < *m_pdwCount; i++)
if (m_pdwIDs[i] == id)
{
MoveMemory(m_pdwIDs + i, m_pdwIDs + i + 1, sizeof(DWORD) * (*m_pdwCount - 1 - i));
(*m_pdwCount)--;
break;
}
// unlock the array
ReleaseMutex(m_hmutex);
}
BOOL
SharedSortedDWORDArray::IsTop(DWORD id)
{
// lock the array
WaitForSingleObject(m_hmutex, INFINITE);
BOOL result = FALSE;
// test last element against id
if (*m_pdwCount)
result = (id == m_pdwIDs[(*m_pdwCount) - 1]);
// unlock array
ReleaseMutex(m_hmutex);
return result;
}