1 #ifndef DOZERG_LOCKED_QUEUE_H_20070901 2 #define DOZERG_LOCKED_QUEUE_H_20070901 24 #include "single_list.hh" 28 template<
class T,
class Container = CSingleList<T> >
35 typedef Container container_type;
36 typedef typename container_type::value_type value_type;
37 typedef typename container_type::size_type size_type;
38 typedef typename container_type::reference reference;
39 typedef typename container_type::iterator iterator;
40 typedef typename container_type::const_reference const_reference;
41 typedef typename container_type::const_iterator const_iterator;
43 static const size_t kCapacityDefault = 10000;
44 template<
class E,
class A>
49 explicit CLockQueue(
size_t capacity = kCapacityDefault)
53 explicit CLockQueue(
const container_type & con,
size_t capacity = kCapacityDefault)
56 , top_size_(con.size())
58 size_type capacity()
const{
return capacity_;}
59 void capacity(size_type c){capacity_ = c;}
60 bool empty()
const volatile{
64 size_type size()
const volatile{
69 bool push(const_reference v, int32_t timeMs = -1){
71 const int sig = waitNotFull(timeMs, 1);
75 const size_t sz = con_.size();
82 bool pushFront(const_reference v, int32_t timeMs = -1){
84 const int sig = waitNotFull(timeMs, 1);
88 const size_t sz = con_.size();
97 bool pushAll(container_type & con, int32_t timeMs = -1){
101 const int sig = waitNotFull(timeMs, con.size());
105 const size_t sz = con_.size();
108 not_empty_.broadcast();
112 bool pop(reference v, int32_t timeMs = -1){
114 const int sig = waitNotEmpty(timeMs, 1);
124 bool popAll(container_type & con, int32_t timeMs = -1){
127 const int sig = waitNotEmpty(timeMs, (con_.empty() ? capacity_ : con_.size()));
131 not_full_.broadcast();
134 size_t topSize()
const volatile{
138 size_t resetTopSize()
volatile{
140 size_t ret = top_size_;
141 top_size_ = c().size();
151 int waitNotEmpty(int32_t timeMs,
size_t need){
156 not_empty_.wait(lock_);
157 }
else if(!timeMs || !not_empty_.timeWait(lock_, timeMs))
160 need = (con_.size() <= need ? 0 : con_.size() - need);
161 return (con_.size() >= capacity_ && need < capacity_ ? 1 : 0);
169 int waitNotFull(int32_t timeMs,
size_t need){
173 while(con_.size() + need > capacity_){
175 not_full_.wait(lock_);
176 }
else if(!timeMs || !not_full_.timeWait(lock_, timeMs))
179 return (con_.empty() ? 1 : 0);
181 container_type & c()
volatile{
return const_cast<container_type &
>(con_);}
182 const container_type & c()
const volatile{
return const_cast<const container_type &
>(con_);}
184 __Myt & operator =(
const __Myt &);
Definition: single_list.hh:28
Definition: lock_queue.hh:29