📄 cdataadmin.cpp
字号:
// 双缓冲队列之间的互斥操作
// CDataAdmin.cpp
//
#include "stdafx.h"
#include "CDataAdmin.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////////////////////////////////
CDataAdmin::CDataAdmin()
{
m_bFlushing = false;
// Allocate buffers
m_pMutex = new CMutex(FALSE, NULL);//互斥对象
for (int i = 0; i < PACK_TOTAL_COUNT; i++)
PoolList.AddTail(new MPEG1_PACK);
// Initialize thread event
m_hBufEnough = ::CreateEvent(NULL, // pointer to security attributes
TRUE, // flag for manual-reset event
FALSE, // flag for initial state
NULL); // pointer to event-object name
}
CDataAdmin::~CDataAdmin()
{
// Free data buffers
PMPEG1_PACK pData;
POSITION pos = DataList.GetHeadPosition();//获得队列头位置
while (pos)
{
pData = (PMPEG1_PACK) DataList.GetNext(pos);
delete pData;
}
pos = PoolList.GetHeadPosition();
while (pos)
{
pData = (PMPEG1_PACK) PoolList.GetNext(pos);
delete pData;
}
PoolList.RemoveAll();//删除对列释放内存
DataList.RemoveAll();
// Free mutex memory
if (m_pMutex != NULL)
{
delete m_pMutex;
m_pMutex = NULL;
}
if (m_hBufEnough)
{
CloseHandle(m_hBufEnough);
m_hBufEnough = NULL;
}
}
//重置列表
int CDataAdmin::ResetList(void)
{
// Initialize the buffer pool
CSingleLock lock(m_pMutex);
lock.Lock(1000);
if (lock.IsLocked())
{
while (!DataList.IsEmpty())
{
PoolList.AddTail(DataList.RemoveHead());
}
lock.Unlock();
}
return 1;
}
//获得列表总长度
int CDataAdmin::GetListSize(void)
{
return (DataList.GetCount() + PoolList.GetCount());
}
// Point to the data list head
PMPEG1_PACK CDataAdmin::PointToDataHead(void)
{
if (!DataList.IsEmpty())
return DataList.GetHead();//获取数据缓冲队列头
return NULL;
}
// Roll back the data buffer
int CDataAdmin::RollBackDataHead(PMPEG1_PACK pData)//回收读完的数据缓冲队列头
{
PMPEG1_PACK pPack = GetDataBuffer();
if (pPack != NULL)
ReleaseDataBuffer(pPack);
return 1;
}
// Get a data buffer to read from...
PMPEG1_PACK CDataAdmin::GetDataBuffer(void)
{
CSingleLock lock(m_pMutex);//互斥对象
PMPEG1_PACK Result = NULL;
lock.Lock(1000);//占有互斥
if (lock.IsLocked())
{
if (!DataList.IsEmpty())
Result = DataList.RemoveHead();//从数据缓冲队列头删除数据包
lock.Unlock();//释放互斥
}
return Result;
}
// Replace the buffer to pool list to receive data...
int CDataAdmin::ReleaseDataBuffer(PMPEG1_PACK pData)//释放数据缓冲队列头
{
CSingleLock lock(m_pMutex);
int nResult = 0;
lock.Lock(1000);
if (lock.IsLocked())
{
if (pData != NULL)
{
PoolList.AddTail(pData);//将读完的数据缓冲队列头加到空闲缓冲队列尾部
nResult = 1;
}
lock.Unlock();
}
return nResult;
}
// Get a pool buffer to receive data...
PMPEG1_PACK CDataAdmin::GetPoolBuffer(void)
{
CSingleLock lock(m_pMutex);
PMPEG1_PACK Result = NULL;
lock.Lock(1000);
if (lock.IsLocked())
{
if (!PoolList.IsEmpty())
Result = PoolList.RemoveHead();//从空闲缓冲队列头删除数据包
lock.Unlock();
}
return Result;
}
// Place the buffer to data list after receiving data...
int CDataAdmin::ReleasePoolBuffer(PMPEG1_PACK pData)
{
CSingleLock lock(m_pMutex);
int nResult = 0;
lock.Lock(1000);
if (lock.IsLocked())
{
if (pData != NULL)
{
DataList.AddTail(pData);//将空闲缓冲队列头数据包加到数据缓冲队列尾部
nResult = 1;
}
lock.Unlock();
}
return nResult;
}
// If no more data available, waiting...
DWORD CDataAdmin::WaitForNext(DWORD inTimeOut)
{
//阻塞函数,m_hBufEnough等待对象,inTimeOut等待时间
return ::WaitForSingleObject(m_hBufEnough, inTimeOut);
}
void CDataAdmin::SetFlushing(bool inFlush)
{
m_bFlushing = inFlush;
}
bool CDataAdmin::IsFlushing(void)
{
return m_bFlushing;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -