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

RMCast_Copy_On_Write.cpp

Go to the documentation of this file.
00001 // $Id: RMCast_Copy_On_Write.cpp,v 1.1.1.1 2001/12/04 14:33:17 chad Exp $
00002 
00003 #ifndef ACE_RMCAST_COPY_ON_WRITE_CPP
00004 #define ACE_RMCAST_COPY_ON_WRITE_CPP
00005 
00006 #include "RMCast_Copy_On_Write.h"
00007 
00008 #if ! defined (__ACE_INLINE__)
00009 #include "RMCast_Copy_On_Write.i"
00010 #endif /* __ACE_INLINE__ */
00011 
00012 ACE_RCSID(RMCast, RMCast_Copy_On_Write, "$Id: RMCast_Copy_On_Write.cpp,v 1.1.1.1 2001/12/04 14:33:17 chad Exp $")
00013 
00014 template<class COLLECTION, class ITERATOR> void
00015 ACE_RMCast_Copy_On_Write_Collection<COLLECTION,ITERATOR>::_incr_refcnt (void)
00016 {
00017   // LOCKING: no locking is required, the caller grabs the mutex.
00018   this->refcount_++;
00019 }
00020 
00021 template<class COLLECTION, class ITERATOR> void
00022 ACE_RMCast_Copy_On_Write_Collection<COLLECTION,ITERATOR>::_decr_refcnt (void)
00023 {
00024   // LOCKING: no locking is required, the caller grabs the mutex.
00025   {
00026     this->refcount_--;
00027     if (this->refcount_ != 0)
00028       return;
00029   }
00030   //@@ TODO: If this wrapper is going to be completely general some
00031   // kind of functor has to be provided to remove the elements in the
00032   // collection, in case the are no self-managed
00033 
00034   delete this;
00035 }
00036 
00037 // ****************************************************************
00038 
00039 template<class KEY, class ITEM, class COLLECTION, class ITERATOR>
00040 
00041 ACE_RMCast_Copy_On_Write<KEY,ITEM,COLLECTION,ITERATOR>::    ACE_RMCast_Copy_On_Write (void)
00042       : ACE_RMCast_Copy_On_Write_Container<COLLECTION,ITERATOR> ()
00043 {
00044 }
00045 
00046 template<class KEY, class ITEM, class COLLECTION, class ITERATOR>
00047 
00048 ACE_RMCast_Copy_On_Write<KEY,ITEM,COLLECTION,ITERATOR>::    ~ACE_RMCast_Copy_On_Write (void)
00049 {
00050   ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->mutex_);
00051 
00052   while (this->pending_writes_ != 0)
00053     this->cond_.wait ();
00054 
00055   this->collection_->_decr_refcnt ();
00056   this->collection_ = 0;
00057 }
00058 
00059 template<class KEY, class ITEM, class COLLECTION, class ITERATOR> int
00060 
00061 ACE_RMCast_Copy_On_Write<KEY,ITEM,COLLECTION,ITERATOR>::    for_each (ACE_RMCast_Worker<KEY,ITEM> *worker)
00062 {
00063   Read_Guard ace_mon (*this);
00064 
00065   ITERATOR end = ace_mon.collection->collection.end ();
00066   for (ITERATOR i = ace_mon.collection->collection.begin (); i != end; ++i)
00067     {
00068       int r = worker->work ((*i).key (), (*i).item ());
00069       if (r == 1)
00070         return 0; // Abort loop, but no error
00071       if (r == -1)
00072         return -1;
00073     }
00074   return 0;
00075 }
00076 
00077 template<class KEY, class ITEM, class C, class ITERATOR> KEY
00078 ACE_RMCast_Copy_On_Write<KEY,ITEM,C,ITERATOR>::first_key (void)
00079 {
00080   Read_Guard ace_mon (*this);
00081   ITERATOR end = ace_mon.collection->collection.end ();
00082   ITERATOR begin = ace_mon.collection->collection.begin ();
00083   if (begin == end)
00084     {
00085       return KEY ();
00086     }
00087   return (*begin).key ();
00088 }
00089 
00090 template<class KEY, class ITEM, class C, class ITERATOR> int
00091 ACE_RMCast_Copy_On_Write<KEY,ITEM,C,ITERATOR>::empty (void)
00092 {
00093   Read_Guard ace_mon (*this);
00094   ITERATOR end = ace_mon.collection->collection.end ();
00095   ITERATOR begin = ace_mon.collection->collection.begin ();
00096 
00097   return end == begin;
00098 }
00099 
00100 template<class KEY, class ITEM, class C, class I> int
00101 ACE_RMCast_Copy_On_Write<KEY,ITEM,C,I>::bind (KEY const & k,
00102                                               ITEM const & i)
00103 {
00104   Write_Guard ace_mon (*this);
00105 
00106   return this->bind_i (ace_mon, k, i);
00107 }
00108 
00109 template<class KEY, class ITEM, class C, class I> int
00110 ACE_RMCast_Copy_On_Write<KEY,ITEM,C,I>::unbind (KEY const & k)
00111 {
00112   Write_Guard ace_mon (*this);
00113 
00114   return this->unbind_i (ace_mon, k);
00115 }
00116 
00117 template<class KEY, class ITEM, class C, class I> int
00118 ACE_RMCast_Copy_On_Write<KEY,ITEM,C,I>::bind_i (Write_Guard &ace_mon,
00119                                                 KEY const & k,
00120                                                 ITEM const & i)
00121 {
00122   return ace_mon.copy->collection.bind (k, i);
00123 }
00124 
00125 template<class KEY, class ITEM, class C, class I> int
00126 ACE_RMCast_Copy_On_Write<KEY,ITEM,C,I>::unbind_i (Write_Guard &ace_mon,
00127                                                   KEY const & k)
00128 {
00129   return ace_mon.copy->collection.unbind (k);
00130 }
00131 
00132 // ****************************************************************
00133 
00134 template<class COLLECTION, class ITERATOR>
00135 ACE_RMCast_Copy_On_Write_Container<COLLECTION,ITERATOR>::ACE_RMCast_Copy_On_Write_Container (void)
00136   : pending_writes_ (0)
00137   , writing_ (0)
00138   , cond_ (mutex_)
00139 {
00140   ACE_NEW (this->collection_, Collection);
00141 }
00142 
00143 // ****************************************************************
00144 
00145 template<class COLLECTION, class ITERATOR>
00146 
00147 ACE_RMCast_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR>::  ACE_RMCast_Copy_On_Write_Write_Guard (Container &container)
00148   : copy (0)
00149   , mutex (container.mutex_)
00150   , cond (container.cond_)
00151   , pending_writes (container.pending_writes_)
00152   , writing_flag (container.writing_)
00153   , collection (container.collection_)
00154 {
00155   {
00156     ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->mutex);
00157 
00158     this->pending_writes++;
00159 
00160     while (this->writing_flag != 0)
00161       this->cond.wait ();
00162 
00163     this->writing_flag = 1;
00164   }
00165 
00166   // Copy outside the mutex, because it may take a long time.
00167   // Nobody can change it, because it is protected by the
00168   // writing_flag.
00169 
00170   // First initialize it (with the correct reference count
00171   ACE_NEW (this->copy, Collection);
00172   // Copy the contents
00173   this->copy->collection = this->collection->collection;
00174 }
00175 
00176 template<class COLLECTION, class ITERATOR>
00177 
00178 ACE_RMCast_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR>::    ~ACE_RMCast_Copy_On_Write_Write_Guard (void)
00179 {
00180   Collection *tmp = 0;
00181   {
00182     ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, this->mutex);
00183 
00184     tmp = this->collection;
00185     this->collection = this->copy;
00186     this->writing_flag = 0;
00187     this->pending_writes--;
00188 
00189     this->cond.signal ();
00190   }
00191   // Delete outside the mutex, because it may take a long time.
00192   // @@ Is this right?  What happens if several readers are still
00193   // using the old copy?
00194   tmp->_decr_refcnt ();
00195 }
00196 
00197 // ****************************************************************
00198 
00199 #endif /* ACE_RMCAST_COPY_ON_WRITE_CPP */

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