/home/aconway/svn/qpid/cpp/src/qpid/sys/ProducerConsumer.h

00001 #ifndef _sys_ProducerConsumer_h
00002 #define _sys_ProducerConsumer_h
00003 
00004 /*
00005  *
00006  * Copyright (c) 2006 The Apache Software Foundation
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *    http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  *
00020  */
00021 #include <boost/noncopyable.hpp>
00022 #include "qpid/Exception.h"
00023 #include "Monitor.h"
00024 
00025 namespace qpid {
00026 namespace sys {
00027 
00041 class ProducerConsumer
00042 {
00043   public:
00044     ProducerConsumer(size_t init_items=0);
00045 
00046     ~ProducerConsumer() { shutdown(); }
00047 
00052     void shutdown();
00053 
00055     bool isShutdown() { return shutdownFlag; }
00056 
00058     size_t available() const;
00059 
00061     size_t consumers() const;
00062 
00064     bool empty() const { return available() == 0; }
00065 
00069     class Lock : private boost::noncopyable {
00070       public:
00071 
00082         bool isOk() const;
00083 
00088         void confirm();
00089 
00096         void cancel();
00097 
00099         bool isTimedOut() const { return status == TIMEOUT; }
00100 
00102         bool isShutdown() const { return pc.isShutdown(); }
00103         
00104         ProducerConsumer& pc;
00105 
00106       protected:
00108         enum Status { INCOMPLETE, CONFIRMED, CANCELLED, TIMEOUT };
00109 
00110         Lock(ProducerConsumer& p);
00111         ~Lock();
00112         void checkOk() const;
00113         Mutex::ScopedLock lock;
00114         Status status;
00115     };
00116     
00118     struct ProducerLock : public Lock {
00124         ProducerLock(ProducerConsumer& p);
00125 
00127         ~ProducerLock();
00128     };
00129 
00131     struct ConsumerLock : public Lock {
00138         ConsumerLock(ProducerConsumer& p);
00139 
00146         ConsumerLock(ProducerConsumer& p, const Time& timeout);
00147 
00149         ~ConsumerLock();
00150     };
00151 
00152   private:
00153     mutable Monitor monitor;
00154     size_t items;
00155     size_t waiters;
00156     bool shutdownFlag;
00157 
00158   friend class Lock;
00159   friend class ProducerLock;
00160   friend class ConsumerLock;
00161 };
00162 
00163 }} // namespace qpid::sys
00164 
00165 #endif  

Generated on Tue Apr 17 14:22:03 2007 for Qpid by  doxygen 1.4.7