common/include/pion/PionLockedQueue.hpp

00001 // -----------------------------------------------------------------------
00002 // pion-common: a collection of common libraries used by the Pion Platform
00003 // -----------------------------------------------------------------------
00004 // Copyright (C) 2007-2008 Atomic Labs, Inc.  (http://www.atomiclabs.com)
00005 //
00006 // Distributed under the Boost Software License, Version 1.0.
00007 // See http://www.boost.org/LICENSE_1_0.txt
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 // NOTE: the data structures contained in this file are based upon algorithms
00028 // published in the paper "Simple, Fast, and Practical Non-Blocking and Blocking
00029 // Concurrent Queue Algorithms" (1996, Maged M. Michael and Michael L. Scott,
00030 // Department of Computer Science, University of Rochester).
00031 // See http://www.cs.rochester.edu/u/scott/papers/1996_PODC_queues.pdf
00032 
00033 
00034 namespace pion {    // begin 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;       //< data wrapped by the node item
00051         QueueNode * next;       //< points to the next node in the queue
00052         boost::uint32_t version;    //< the node item's version number
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         // just return if the list is empty
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         // get a copy of the item at the head of the list
00092         version = new_head_ptr->version;
00093         t = new_head_ptr->data;
00094 
00095         // update the pointer to the head of the list
00096         QueueNode *old_head_ptr = m_head_ptr;
00097         m_head_ptr = new_head_ptr;
00098         head_lock.unlock();
00099 
00100         // free the QueueNode for the old head of the list
00101         destroyNode(old_head_ptr);
00102 
00103         // decrement size
00104         --m_size;
00105 
00106         // item successfully dequeued
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;       //< true while the thread is running/active
00135         boost::condition    m_wakeup_event; //< triggered when a new item is available
00136         ConsumerThread *    m_next_ptr;     //< pointer to the next idle thread
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         // initialize with a dummy node since m_head_ptr is always 
00146         // pointing to the item before the head of the list
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         // sleep while MaxSize is exceeded
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         // create a new list node for the queue item
00196         QueueNode *node_ptr = createNode();
00197         node_ptr->data = t;
00198         node_ptr->next = NULL;
00199         node_ptr->version = 0;
00200 
00201         // append node to the end of the list
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         // update the tail pointer for the new node
00207         m_tail_ptr = node_ptr;
00208 
00209         // increment size
00210         ++m_size;
00211 
00212         // wake up an idle thread (if any)
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             // try to get the next value
00234             if ( dequeue(t, last_known_version) )
00235                 return true;    // got an item
00236 
00237             // queue is empty
00238             boost::mutex::scoped_lock tail_lock(m_tail_mutex);
00239             if (m_tail_ptr->version == last_known_version) {
00240                 // still empty after acquiring lock
00241                 thread_info.m_next_ptr = m_idle_ptr;
00242                 m_idle_ptr = & thread_info;
00243                 // wait for an item to become available
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 }   // end namespace pion
00291 
00292 #endif

Generated on Fri Apr 30 14:48:53 2010 for pion-net by  doxygen 1.4.7