forked from grishka/libtgvoip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingQueue.cpp
More file actions
84 lines (70 loc) · 1.51 KB
/
Copy pathBlockingQueue.cpp
File metadata and controls
84 lines (70 loc) · 1.51 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
// libtgvoip is free and unencumbered public domain software.
// For more information, see http://unlicense.org or the UNLICENSE file
// you should have received with this source code distribution.
//
#include "BlockingQueue.h"
CBlockingQueue::CBlockingQueue(size_t capacity){
this->capacity=capacity;
overflowCallback=NULL;
init_lock(lock);
init_mutex(mutex);
}
CBlockingQueue::~CBlockingQueue(){
lock_mutex(mutex);
notify_lock(lock);
unlock_mutex(mutex);
lock_mutex(mutex);
unlock_mutex(mutex);
free_lock(lock);
free_mutex(mutex);
}
void CBlockingQueue::Put(void *thing){
lock_mutex(mutex);
if(queue.empty()){
notify_lock(lock);
}
queue.push_back(thing);
while(queue.size()>capacity){
if(overflowCallback){
overflowCallback(queue.front());
queue.pop_front();
}else{
abort();
}
}
unlock_mutex(mutex);
}
void *CBlockingQueue::GetBlocking(){
lock_mutex(mutex);
while(queue.empty()){
wait_lock(lock, mutex);
}
void* r=GetInternal();
unlock_mutex(mutex);
return r;
}
void *CBlockingQueue::Get(){
lock_mutex(mutex);
void* r=GetInternal();
unlock_mutex(mutex);
return r;
}
void *CBlockingQueue::GetInternal(){
if(queue.size()==0)
return NULL;
void* r=queue.front();
queue.pop_front();
return r;
}
unsigned int CBlockingQueue::Size(){
return queue.size();
}
void CBlockingQueue::PrepareDealloc(){
lock_mutex(mutex);
notify_lock(lock);
unlock_mutex(mutex);
}
void CBlockingQueue::SetOverflowCallback(void (*overflowCallback)(void *)){
this->overflowCallback=overflowCallback;
}