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         }
00175         m_tail_ptr = m_head_ptr;
00176     }
00177 
00183     void push(const T& t) {
00184         // sleep while MaxSize is exceeded
00185         if (MaxSize > 0) {
00186             boost::system_time wakeup_time;
00187             while (size() >= MaxSize) {
00188                 wakeup_time = boost::get_system_time()
00189                     + boost::posix_time::millisec(SleepMilliSec);
00190                 boost::thread::sleep(wakeup_time);
00191             }
00192         }
00193 
00194         // create a new list node for the queue item
00195         QueueNode *node_ptr = createNode();
00196         node_ptr->data = t;
00197         node_ptr->next = NULL;
00198         node_ptr->version = 0;
00199 
00200         // append node to the end of the list
00201         boost::mutex::scoped_lock tail_lock(m_tail_mutex);
00202         node_ptr->version = (m_next_version += 2);
00203         m_tail_ptr->next = node_ptr;
00204 
00205         // update the tail pointer for the new node
00206         m_tail_ptr = node_ptr;
00207 
00208         // increment size
00209         ++m_size;
00210 
00211         // wake up an idle thread (if any)
00212         if (m_idle_ptr) {
00213             ConsumerThread *idle_ptr = m_idle_ptr;
00214             m_idle_ptr = m_idle_ptr->m_next_ptr;
00215             idle_ptr->m_wakeup_event.notify_one();
00216         }
00217     }
00218     
00229     bool pop(T& t, ConsumerThread& thread_info) {
00230         boost::uint32_t last_known_version;
00231         while (thread_info.isRunning()) {
00232             // try to get the next value
00233             if ( dequeue(t, last_known_version) )
00234                 return true;    // got an item
00235 
00236             // queue is empty
00237             boost::mutex::scoped_lock tail_lock(m_tail_mutex);
00238             if (m_tail_ptr->version == last_known_version) {
00239                 // still empty after acquiring lock
00240                 thread_info.m_next_ptr = m_idle_ptr;
00241                 m_idle_ptr = & thread_info;
00242                 // wait for an item to become available
00243                 thread_info.m_wakeup_event.wait(tail_lock);
00244             }
00245         }
00246         return false;
00247     }
00248 
00256     inline bool pop(T& t) { boost::uint32_t version; return dequeue(t, version); }
00257     
00258 
00259 private:
00260 
00261 #if defined(PION_HAVE_LOCKFREE) && !defined(_MSC_VER)
00263     boost::lockfree::caching_freelist<QueueNode>    m_free_list;
00264 #endif
00265     
00267     boost::mutex                    m_head_mutex;
00268 
00270     boost::mutex                    m_tail_mutex;
00271 
00273     QueueNode *                     m_head_ptr;
00274 
00276     QueueNode *                     m_tail_ptr;
00277 
00279     ConsumerThread *                m_idle_ptr;
00280 
00282     boost::uint32_t                 m_next_version;
00283     
00285     boost::detail::atomic_count     m_size;
00286 };
00287 
00288     
00289 }   // end namespace pion
00290 
00291 #endif

Generated on Fri Dec 4 08:54:29 2009 for pion-net by  doxygen 1.4.7