Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

RMCast_Copy_On_Write.h

Go to the documentation of this file.
00001 /* -*- C++ -*- */
00002 // $Id: RMCast_Copy_On_Write.h,v 1.1.1.2 2003/02/21 18:36:32 chad Exp $
00003 
00004 #ifndef ACE_RMCAST_COPY_ON_WRITE_H
00005 #define ACE_RMCAST_COPY_ON_WRITE_H
00006 #include "ace/pre.h"
00007 
00008 #include "RMCast_Worker.h"
00009 #include "ace/Synch.h"
00010 
00011 //! A wrapper to implement reference counted collections
00012 template<class COLLECTION, class ITERATOR>
00013 class ACE_RMCast_Copy_On_Write_Collection
00014 {
00015 public:
00016   //! Constructor
00017   ACE_RMCast_Copy_On_Write_Collection (void);
00018 
00019   //! Increment the reference count
00020   void _incr_refcnt (void);
00021 
00022   //! Decrement the reference count
00023   void _decr_refcnt (void);
00024 
00025   //! The actual collection
00026   COLLECTION collection;
00027 
00028 private:
00029   //! The reference count
00030   ACE_UINT32 refcount_;
00031 };
00032 
00033 // ****************************************************************
00034 
00035 template<class COLLECTION, class ITERATOR>
00036 class ACE_RMCast_Copy_On_Write_Read_Guard;
00037 
00038 template<class COLLECTION, class ITERATOR>
00039 class ACE_RMCast_Copy_On_Write_Write_Guard;
00040 
00041 //! Base class for the Copy_On_Write collection, used to simplify the
00042 //! declaration of the Write_Guard
00043 template<class COLLECTION, class ITERATOR>
00044 class ACE_RMCast_Copy_On_Write_Container
00045 {
00046 public:
00047   //! Constructor
00048   ACE_RMCast_Copy_On_Write_Container (void);
00049 
00050   //! Let the Write_Guard access the internal fields.
00051   friend class ACE_RMCast_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR>;
00052 
00053   //! Let the Read_Guard access the internal fields.
00054   friend class ACE_RMCast_Copy_On_Write_Read_Guard<COLLECTION,ITERATOR>;
00055 
00056   //! A shorter name for the actual collection type
00057   typedef ACE_RMCast_Copy_On_Write_Collection<COLLECTION,ITERATOR> Collection;
00058 
00059 protected:
00060   //! Number of pending writes
00061   int pending_writes_;
00062 
00063   //! If non-zero then a thread is changing the collection.
00064   /*!
00065    * Many threads can use the collection simulatenously, but only one
00066    * change it.
00067    */
00068   int writing_;
00069 
00070   //! A mutex to serialize access to the collection pointer.
00071   ACE_SYNCH_MUTEX mutex_;
00072 
00073   //! A condition variable to wait to synchronize multiple writers.
00074   ACE_SYNCH_CONDITION cond_;
00075 
00076   //! The collection, with reference counting added
00077   Collection *collection_;
00078 };
00079 
00080 // ****************************************************************
00081 
00082 //! Implement a read guard for a reference counted collection
00083 template<class COLLECTION, class ITERATOR>
00084 class ACE_RMCast_Copy_On_Write_Read_Guard
00085 {
00086 public:
00087   typedef ACE_RMCast_Copy_On_Write_Collection<COLLECTION,ITERATOR> Collection;
00088   typedef ACE_RMCast_Copy_On_Write_Container<COLLECTION,ITERATOR> Container;
00089 
00090   //! Constructor
00091   ACE_RMCast_Copy_On_Write_Read_Guard (Container &container);
00092 
00093   //! Destructor
00094   ~ACE_RMCast_Copy_On_Write_Read_Guard (void);
00095 
00096   //! A reference to the collection
00097   Collection *collection;
00098 
00099 private:
00100   //! Synchronization
00101   ACE_SYNCH_MUTEX &mutex_;
00102 };
00103 
00104 // ****************************************************************
00105 
00106 //! Implement the write guard for a reference counted collecion
00107 /*!
00108  * This helper class atomically increments the reference count of a
00109  * ACE_RMCast_Copy_On_Write_Collection and reads the current
00110  * collection in the Copy_On_Write class.
00111  */
00112 template<class COLLECTION, class ITERATOR>
00113 class ACE_RMCast_Copy_On_Write_Write_Guard
00114 {
00115 public:
00116   typedef ACE_RMCast_Copy_On_Write_Collection<COLLECTION,ITERATOR> Collection;
00117   typedef ACE_RMCast_Copy_On_Write_Container<COLLECTION,ITERATOR> Container;
00118 
00119   //! Constructor
00120   ACE_RMCast_Copy_On_Write_Write_Guard (Container &container);
00121 
00122   //! Destructor
00123   ~ACE_RMCast_Copy_On_Write_Write_Guard (void);
00124 
00125   //! The collection
00126   Collection *copy;
00127 
00128 private:
00129   //! Keep a reference to the mutex
00130   ACE_SYNCH_MUTEX &mutex;
00131 
00132   //! Keep a reference to the condition variable
00133   ACE_SYNCH_CONDITION &cond;
00134 
00135   //! Use a reference to update the pending writes count
00136   int &pending_writes;
00137 
00138   //! Use a reference to update the writing flag
00139   int &writing_flag;
00140 
00141   //! Use this reference to update the collection once the
00142   //! modifications are finished.
00143   Collection *&collection;
00144 };
00145 
00146 
00147 // ****************************************************************
00148 
00149 //! Implement a copy on write wrapper for a map-like collection
00150 /*
00151  *
00152  * <B>WARNING: </B>  This class may be moved away in the future, I'm
00153  * investigating how it could be converted into a reusable component
00154  * in ACE.   I won't make promises on when will that happen, but I
00155  * won't promise that it will stay here either.
00156  *
00157  */
00158 template<class KEY, class ITEM, class COLLECTION, class ITERATOR>
00159 class ACE_RMCast_Copy_On_Write : public ACE_RMCast_Copy_On_Write_Container<COLLECTION,ITERATOR>
00160 {
00161 public:
00162   //! The Read_Guard trait
00163   typedef ACE_RMCast_Copy_On_Write_Read_Guard<COLLECTION,ITERATOR> Read_Guard;
00164 
00165   //! The Write_Guard trait
00166   typedef ACE_RMCast_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR> Write_Guard;
00167 
00168   //! The underlying collection type
00169   typedef ACE_RMCast_Copy_On_Write_Collection<COLLECTION,ITERATOR> Collection;
00170 
00171   //! Constructor
00172   ACE_RMCast_Copy_On_Write (void);
00173 
00174   //! Destructor
00175   ~ACE_RMCast_Copy_On_Write (void);
00176 
00177   //! Iterate over all the elements invoking \param worker on each one.
00178   int for_each (ACE_RMCast_Worker<KEY,ITEM> *worker);
00179 
00180   //! Get the first key
00181   KEY first_key (void);
00182 
00183   /// Return non-zero if the collection is empty
00184   int empty (void);
00185 
00186   //! Add a new element
00187   int bind (KEY const & key, ITEM const & item);
00188 
00189   //! Remove an element
00190   int unbind (KEY const & key);
00191 
00192   //! Bind assuming the Write_Guard is held
00193   int bind_i (Write_Guard &guard, KEY const & key, ITEM const & item);
00194 
00195   //! Unbind assuming the Write_Guard is held
00196   int unbind_i (Write_Guard &guard, KEY const & key);
00197 };
00198 
00199 #if defined (__ACE_INLINE__)
00200 #include "RMCast_Copy_On_Write.i"
00201 #endif /* __ACE_INLINE__ */
00202 
00203 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00204 #include "RMCast_Copy_On_Write.cpp"
00205 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00206 
00207 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00208 #pragma implementation ("RMCast_Copy_On_Write.cpp")
00209 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00210 
00211 #include "ace/post.h"
00212 #endif /* ACE_RMCAST_COPY_ON_WRITE_H */

Generated on Mon Jun 16 13:12:35 2003 for ACE_RMCast by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002