-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferQueue.cpp
More file actions
43 lines (37 loc) · 844 Bytes
/
bufferQueue.cpp
File metadata and controls
43 lines (37 loc) · 844 Bytes
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
#include <assert.h>
#include "common/common.h"
#include "mutexLock.h"
#include "condition.h"
#include "task.h"
#include "bufferQueue.h"
USING_NAMESPACE(std)
NAMESPACE_SETUP(Util)
BufferQueue::BufferQueue(size_t maxSize)
:mSize(maxSize),
mEmptyCondition(mMutexLock),
mFullCondition(mMutexLock) {
assert(maxSize);
}
BufferQueue::~BufferQueue() {
}
void BufferQueue::Push(Task* task) {
mMutexLock.Lock();
while (mQueue.size() >= mSize) {
mEmptyCondition.Wait();
}
mQueue.push(task);
mMutexLock.UnLock();
mFullCondition.Notify();
}
Task* BufferQueue::Pop() {
mMutexLock.Lock();
while (mQueue.empty()) {
mFullCondition.Wait();
}
Task* task = mQueue.front();
mQueue.pop();
mMutexLock.UnLock();
mEmptyCondition.Notify();
return task;
}
NAMESPACE_END(Util)