Sirikata
|
00001 /* Sirikata Utilities -- Sirikata Synchronization Utilities 00002 * ThreadSafeQueueWithNotification.hpp 00003 * 00004 * Copyright (c) 2010, Ewen Cheslack-Postava 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are 00009 * met: 00010 * * Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in 00014 * the documentation and/or other materials provided with the 00015 * distribution. 00016 * * Neither the name of Sirikata nor the names of its contributors may 00017 * be used to endorse or promote products derived from this software 00018 * without specific prior written permission. 00019 * 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 00021 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 00022 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 00023 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 00024 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00025 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00026 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 */ 00032 00033 #ifndef _SIRIKATA_THREAD_SAFE_QUEUE_WITH_NOTIFICATION_HPP_ 00034 #define _SIRIKATA_THREAD_SAFE_QUEUE_WITH_NOTIFICATION_HPP_ 00035 00036 #include "ThreadSafeQueue.hpp" 00037 00038 namespace Sirikata { 00039 00060 template <typename T> 00061 class ThreadSafeQueueWithNotification { 00062 public: 00063 typedef std::tr1::function<void()> Notification; 00064 00069 ThreadSafeQueueWithNotification(const Notification& cb) 00070 : mQueue(), 00071 mCallback(cb) 00072 { 00073 } 00074 00075 ~ThreadSafeQueueWithNotification() { 00076 } 00077 00079 void push(const T &value) { 00080 // We get the new size from the push, which is locked so we know exactly 00081 // how big the queue is immediately after the push, guaranteeing we 00082 // don't miss it going empty. 00083 int32 new_size = mQueue.push(value); 00084 if (new_size == 1) 00085 mCallback(); 00086 } 00087 00089 void pushMultiple(const std::deque<T> &values) { 00090 int32 nelements = values.size(); 00091 int32 new_size = mQueue.pushMultiple(values); 00092 if (new_size == nelements) 00093 mCallback(); 00094 } 00095 00097 bool pop(T& ret) { 00098 return mQueue.pop(ret); 00099 } 00100 00102 void blockingPop(T& retval) { 00103 mQueue.blockingPop(retval); 00104 } 00105 00107 void swap(std::deque<T>& swapWith) { 00108 mQueue.swap(swapWith); 00109 } 00110 00112 void popAll(std::deque<T> *popResults) { 00113 mQueue.popAll(popResults); 00114 } 00115 00117 bool probablyEmpty() { 00118 return mQueue.probablyEmpty(); 00119 } 00120 00122 bool empty() { 00123 return probablyEmpty(); 00124 } 00125 00130 int32 size() { 00131 return mQueue.size(); 00132 } 00133 00134 private: 00135 // Disable default constructor -- if you're not specifying a callback you 00136 // shouldn't be using this class. 00137 ThreadSafeQueueWithNotification(); 00138 // Disable assignment and copy 00139 ThreadSafeQueueWithNotification& operator=(const ThreadSafeQueueWithNotification& other); 00140 ThreadSafeQueueWithNotification(const ThreadSafeQueueWithNotification& other); 00141 00142 ThreadSafeQueue<T> mQueue; 00143 Notification mCallback; 00144 }; 00145 00146 } // namespace Sirikata 00147 00148 #endif //_SIRIKATA_THREAD_SAFE_QUEUE_WITH_NOTIFICATION_HPP_