00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef __PION_PIONLOCKEDQUEUE_HEADER__
00011 #define __PION_PIONLOCKEDQUEUE_HEADER__
00012
00013 #include <new>
00014 #include <boost/cstdint.hpp>
00015 #include <boost/noncopyable.hpp>
00016 #include <boost/thread/thread.hpp>
00017 #include <boost/thread/mutex.hpp>
00018 #include <boost/thread/condition.hpp>
00019 #include <boost/detail/atomic_count.hpp>
00020 #include <pion/PionConfig.hpp>
00021 #include <pion/PionException.hpp>
00022 #if defined(PION_HAVE_LOCKFREE) && !defined(_MSC_VER)
00023 #include <boost/lockfree/detail/freelist.hpp>
00024 #endif
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 namespace pion {
00035
00036
00040 template <typename T,
00041 boost::uint32_t MaxSize = 250000,
00042 boost::uint32_t SleepMilliSec = 10 >
00043 class PionLockedQueue :
00044 private boost::noncopyable
00045 {
00046 protected:
00047
00049 struct QueueNode {
00050 T data;
00051 QueueNode * next;
00052 boost::uint32_t version;
00053 };
00054
00056 inline QueueNode *createNode(void) {
00057 #if defined(PION_HAVE_LOCKFREE) && !defined(_MSC_VER)
00058 return new (m_free_list.allocate()) QueueNode();
00059 #else
00060 return new QueueNode();
00061 #endif
00062 }
00063
00065 inline void destroyNode(QueueNode *node_ptr) {
00066 #if defined(PION_HAVE_LOCKFREE) && !defined(_MSC_VER)
00067 node_ptr->~QueueNode();
00068 m_free_list.deallocate(node_ptr);
00069 #else
00070 delete node_ptr;
00071 #endif
00072 }
00073
00082 inline bool dequeue(T& t, boost::uint32_t& version) {
00083
00084 boost::mutex::scoped_lock head_lock(m_head_mutex);
00085 QueueNode *new_head_ptr = m_head_ptr->next;
00086 if (! new_head_ptr) {
00087 version = m_head_ptr->version;
00088 return false;
00089 }
00090
00091
00092 version = new_head_ptr->version;
00093 t = new_head_ptr->data;
00094
00095
00096 QueueNode *old_head_ptr = m_head_ptr;
00097 m_head_ptr = new_head_ptr;
00098 head_lock.unlock();
00099
00100
00101 destroyNode(old_head_ptr);
00102
00103
00104 --m_size;
00105
00106
00107 return true;
00108 }
00109
00110
00111 public:
00112
00114 class ConsumerThread {
00115 public:
00116
00118 ConsumerThread(void) : m_is_running(true), m_next_ptr(NULL) {}
00119
00121 inline bool isRunning(void) const { return m_is_running; }
00122
00124 inline void stop(void) { m_is_running = false; m_wakeup_event.notify_one(); }
00125
00127 inline void reset(void) { m_is_running = true; m_next_ptr = NULL; }
00128
00129 private:
00130
00132 friend class PionLockedQueue;
00133
00134 volatile bool m_is_running;
00135 boost::condition m_wakeup_event;
00136 ConsumerThread * m_next_ptr;
00137 };
00138
00139
00141 PionLockedQueue(void)
00142 : m_head_ptr(NULL), m_tail_ptr(NULL), m_idle_ptr(NULL),
00143 m_next_version(1), m_size(0)
00144 {
00145
00146
00147 m_head_ptr = m_tail_ptr = createNode();
00148 m_head_ptr->next = NULL;
00149 m_head_ptr->version = 0;
00150 }
00151
00153 virtual ~PionLockedQueue() {
00154 clear();
00155 destroyNode(m_tail_ptr);
00156 }
00157
00159 inline bool empty(void) const { return (m_head_ptr->next == NULL); }
00160
00162 std::size_t size(void) const {
00163 return m_size;
00164 }
00165
00167 void clear(void) {
00168 boost::mutex::scoped_lock tail_lock(m_tail_mutex);
00169 boost::mutex::scoped_lock head_lock(m_head_mutex);
00170 while (m_head_ptr->next) {
00171 m_tail_ptr = m_head_ptr;
00172 m_head_ptr = m_head_ptr->next;
00173 destroyNode(m_tail_ptr);
00174 --m_size;
00175 }
00176 m_tail_ptr = m_head_ptr;
00177 }
00178
00184 void push(const T& t) {
00185
00186 if (MaxSize > 0) {
00187 boost::system_time wakeup_time;
00188 while (size() >= MaxSize) {
00189 wakeup_time = boost::get_system_time()
00190 + boost::posix_time::millisec(SleepMilliSec);
00191 boost::thread::sleep(wakeup_time);
00192 }
00193 }
00194
00195
00196 QueueNode *node_ptr = createNode();
00197 node_ptr->data = t;
00198 node_ptr->next = NULL;
00199 node_ptr->version = 0;
00200
00201
00202 boost::mutex::scoped_lock tail_lock(m_tail_mutex);
00203 node_ptr->version = (m_next_version += 2);
00204 m_tail_ptr->next = node_ptr;
00205
00206
00207 m_tail_ptr = node_ptr;
00208
00209
00210 ++m_size;
00211
00212
00213 if (m_idle_ptr) {
00214 ConsumerThread *idle_ptr = m_idle_ptr;
00215 m_idle_ptr = m_idle_ptr->m_next_ptr;
00216 idle_ptr->m_wakeup_event.notify_one();
00217 }
00218 }
00219
00230 bool pop(T& t, ConsumerThread& thread_info) {
00231 boost::uint32_t last_known_version;
00232 while (thread_info.isRunning()) {
00233
00234 if ( dequeue(t, last_known_version) )
00235 return true;
00236
00237
00238 boost::mutex::scoped_lock tail_lock(m_tail_mutex);
00239 if (m_tail_ptr->version == last_known_version) {
00240
00241 thread_info.m_next_ptr = m_idle_ptr;
00242 m_idle_ptr = & thread_info;
00243
00244 thread_info.m_wakeup_event.wait(tail_lock);
00245 }
00246 }
00247 return false;
00248 }
00249
00257 inline bool pop(T& t) { boost::uint32_t version; return dequeue(t, version); }
00258
00259
00260 private:
00261
00262 #if defined(PION_HAVE_LOCKFREE) && !defined(_MSC_VER)
00264 boost::lockfree::caching_freelist<QueueNode> m_free_list;
00265 #endif
00266
00268 boost::mutex m_head_mutex;
00269
00271 boost::mutex m_tail_mutex;
00272
00274 QueueNode * m_head_ptr;
00275
00277 QueueNode * m_tail_ptr;
00278
00280 ConsumerThread * m_idle_ptr;
00281
00283 boost::uint32_t m_next_version;
00284
00286 boost::detail::atomic_count m_size;
00287 };
00288
00289
00290 }
00291
00292 #endif