root/trunk/whisperlib/common/sync/producer_consumer_queue.h

Revision 7, 5.4 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Catalin Popescu
31
32 #ifndef __COMMON_PRODUCER_CONSUMER_QUEUE_H__
33 #define __COMMON_PRODUCER_CONSUMER_QUEUE_H__
34
35 #include <errno.h>
36 #include <pthread.h>
37 #include <deque>
38 #include <whisperlib/common/base/types.h>
39 #include <whisperlib/common/base/log.h>
40 #include <whisperlib/common/base/timer.h>
41
42 namespace synch {
43
44 template<typename C>
45 class ProducerConsumerQueue {
46  public:
47   explicit ProducerConsumerQueue(int max_size)
48     : max_size_(max_size) {
49     CHECK_SYS_FUN(pthread_mutex_init(&mutex_, NULL), 0);
50     CHECK_SYS_FUN(pthread_cond_init(&cond_full_, NULL), 0);
51     CHECK_SYS_FUN(pthread_cond_init(&cond_empty_, NULL), 0);
52   }
53   ~ProducerConsumerQueue() {
54     CHECK_SYS_FUN(pthread_mutex_destroy(&mutex_), 0);
55     CHECK_SYS_FUN(pthread_cond_destroy(&cond_full_), 0);
56     CHECK_SYS_FUN(pthread_cond_destroy(&cond_empty_), 0);
57   }
58
59   void Put(C p) {
60     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
61     while ( data_.size() >= max_size_ ) {
62       CHECK_SYS_FUN(pthread_cond_wait(&cond_empty_, &mutex_), 0);
63     }
64     data_.push_back(p);
65     CHECK_SYS_FUN(pthread_cond_signal(&cond_full_), 0);
66     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
67   }
68   bool Put(C p, uint32 timeout_in_ms) {
69     if ( kInfiniteWait == timeout_in_ms ) {
70       Put(p);
71       return true;
72     }
73     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
74     if ( timeout_in_ms && data_.size() >= max_size_ ) {
75       struct timespec ts = timer::TimespecAbsoluteMsec(timeout_in_ms);
76       const int result = pthread_cond_timedwait(&cond_empty_, &mutex_, &ts);
77       CHECK(result == 0 || result == ETIMEDOUT)
78         << " Invalid result: " << result;
79     }
80     if ( data_.size() >= max_size_ ) {
81       CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
82       return false;
83     }
84     data_.push_back(p);
85     CHECK_SYS_FUN(pthread_cond_signal(&cond_full_), 0);
86     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
87     return true;
88   }
89   C Get() {
90     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
91     while ( data_.empty() ) {
92       CHECK_SYS_FUN(pthread_cond_wait(&cond_full_, &mutex_), 0);
93     }
94     C const ret = data_.front();
95     data_.pop_front();
96     CHECK_SYS_FUN(pthread_cond_signal(&cond_empty_), 0);
97     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
98     return ret;
99   }
100   C Get(uint32 timeout_in_ms) {
101     if ( kInfiniteWait == timeout_in_ms ) {
102       return Get();
103     }
104     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
105     if ( timeout_in_ms && data_.empty() ) {
106       struct timespec ts = timer::TimespecAbsoluteMsec(timeout_in_ms);
107       const int result = pthread_cond_timedwait(&cond_full_, &mutex_, &ts);
108       CHECK(result == 0 || result == ETIMEDOUT)
109         << " Invalid result: " << result;
110     }
111     C ret(NULL);
112     if ( !data_.empty() ) {
113       ret = data_.front();
114       data_.pop_front();
115     }
116     CHECK_SYS_FUN(pthread_cond_signal(&cond_empty_), 0);
117     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
118     return ret;
119   }
120   void GetAll(vector<C>* out) {
121     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
122     while ( !data_.empty() ) {
123       out->push_back(data_.front());
124       data_.pop_front();
125     }
126     CHECK_SYS_FUN(pthread_cond_signal(&cond_empty_), 0);
127     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
128   }
129   bool IsFull() {
130     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
131     const bool is_full (data_.size() >= max_size_);
132     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
133     return is_full;
134   }
135   int32 Size() {
136     CHECK_SYS_FUN(pthread_mutex_lock(&mutex_), 0);
137     const int32 size = data_.size();
138     CHECK_SYS_FUN(pthread_mutex_unlock(&mutex_), 0);
139     return size;
140   }
141
142   static const uint32 kInfiniteWait = 0xffffffff;
143
144  protected:
145   pthread_mutex_t mutex_;
146   pthread_cond_t cond_full_;   // triggered when is some element in queue
147   pthread_cond_t cond_empty_;  // triggered when is some free space in queue
148
149   const int max_size_;
150   deque<C> data_;
151
152  private:
153   DISALLOW_EVIL_CONSTRUCTORS(ProducerConsumerQueue);
154 };
155 }
156
157 #endif  //  __COMMON_PRODUCER_CONSUMER_QUEUR_H__
Note: See TracBrowser for help on using the browser.