-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBufferPool.cpp
62 lines (52 loc) · 1.05 KB
/
BufferPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "BufferPool.h"
BufferPool::BufferPool(UINT uNum)
{
m_pHead= NULL;
m_pDestroyHead= NULL;
m_uMaxBufferNum= uNum;
m_uCurBusyBufferNum= 0;
if(uNum!= 0)
{
m_pHead= (Pnode)malloc(uNum*sizeof(Node));
assert(m_pHead== NULL);
m_pDestroyHead= m_pHead;
for(UINT num= 0; num< uNum-1; num++)
{
m_pHead[num].next= &m_pHead[num+ 1];
}
}
pthread_mutex_init(&m_mutex,NULL);
pthread_cond_init(&m_cond,NULL);
}
Pnode BufferPool::GetNode()
{
pthread_mutex_lock(&m_mutex);
while(m_pHead== NULL)
{
pthread_cond_wait(&m_cond,&m_mutex);
}
Pnode pRet= m_pHead;
m_pHead= m_pHead->next;
m_uCurBusyBufferNum++;
pthread_mutex_unlock(&m_mutex);
return pRet;
}
void BufferPool::RealseNode(Pnode pnode)
{
pthread_mutex_lock(&m_mutex);
pnode->next= m_pHead;
m_pHead= pnode;
m_uCurBusyBufferNum--;
if(pnode->next== NULL)
{
pthread_cond_signal(&m_cond);
}
pthread_mutex_unlock(&m_mutex);
}
void BufferPool::DestroyPool()
{
if(m_pDestroyHead!= NULL)
{
free(m_pDestroyHead);
}
}