ESF_Copy_On_Write.cpp

Go to the documentation of this file.
00001 // ESF_Copy_On_Write.cpp,v 1.10 2006/03/14 06:14:25 jtc Exp
00002 
00003 #ifndef TAO_ESF_COPY_ON_WRITE_CPP
00004 #define TAO_ESF_COPY_ON_WRITE_CPP
00005 
00006 #include "orbsvcs/ESF/ESF_Copy_On_Write.h"
00007 
00008 #if ! defined (__ACE_INLINE__)
00009 #include "orbsvcs/ESF/ESF_Copy_On_Write.i"
00010 #endif /* __ACE_INLINE__ */
00011 
00012 #include "tao/Environment.h"
00013 
00014 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 template<class COLLECTION, class ITERATOR> CORBA::ULong
00017 TAO_ESF_Copy_On_Write_Collection<COLLECTION,ITERATOR>::_incr_refcnt (void)
00018 {
00019   // LOCKING: no locking is required, the caller grabs the mutex.
00020   return this->refcount_++;
00021 }
00022 
00023 template<class COLLECTION, class ITERATOR> CORBA::ULong
00024 TAO_ESF_Copy_On_Write_Collection<COLLECTION,ITERATOR>::_decr_refcnt (void)
00025 {
00026   // LOCKING: no locking is required, the caller grabs the mutex.
00027   {
00028     this->refcount_--;
00029     if (this->refcount_ != 0)
00030       return this->refcount_;
00031   }
00032 
00033   ITERATOR end = this->collection.end ();
00034   for (ITERATOR i = this->collection.begin (); i != end; ++i)
00035     {
00036       (*i)->_decr_refcnt ();
00037     }
00038 
00039   delete this;
00040   return 0;
00041 }
00042 
00043 // ****************************************************************
00044 
00045 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00046 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00047     TAO_ESF_Copy_On_Write (void)
00048       :  pending_writes_ (0),
00049          writing_ (0),
00050          cond_ (mutex_)
00051 {
00052   ACE_NEW (this->collection_, Collection);
00053 }
00054 
00055 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00056 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00057     ~TAO_ESF_Copy_On_Write (void)
00058 {
00059   ACE_GUARD (ACE_SYNCH_MUTEX_T, ace_mon, this->mutex_);
00060 
00061   while (this->pending_writes_ != 0)
00062     this->cond_.wait ();
00063 
00064   this->collection_->_decr_refcnt ();
00065   this->collection_ = 0;
00066 }
00067 
00068 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00069 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00070     for_each (TAO_ESF_Worker<PROXY> *worker
00071               ACE_ENV_ARG_DECL)
00072 {
00073   Read_Guard ace_mon (this->mutex_,
00074                       this->collection_);
00075 
00076   worker->set_size(ace_mon.collection->collection.size());
00077   ITERATOR end = ace_mon.collection->collection.end ();
00078   for (ITERATOR i = ace_mon.collection->collection.begin (); i != end; ++i)
00079     {
00080       worker->work (*i ACE_ENV_ARG_PARAMETER);
00081       ACE_CHECK;
00082     }
00083 }
00084 
00085 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00086 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00087     connected (PROXY *proxy
00088                ACE_ENV_ARG_DECL)
00089 {
00090   Write_Guard ace_mon (this->mutex_,
00091                        this->cond_,
00092                        this->pending_writes_,
00093                        this->writing_,
00094                        this->collection_);
00095 
00096   proxy->_incr_refcnt ();
00097   ace_mon.copy->collection.connected (proxy ACE_ENV_ARG_PARAMETER);
00098 }
00099 
00100 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00101 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00102     reconnected (PROXY *proxy
00103                  ACE_ENV_ARG_DECL)
00104 {
00105   Write_Guard ace_mon (this->mutex_,
00106                        this->cond_,
00107                        this->pending_writes_,
00108                        this->writing_,
00109                        this->collection_);
00110 
00111   proxy->_incr_refcnt ();
00112   ace_mon.copy->collection.reconnected (proxy ACE_ENV_ARG_PARAMETER);
00113 }
00114 
00115 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00116 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00117     disconnected (PROXY *proxy
00118                   ACE_ENV_ARG_DECL)
00119 {
00120   Write_Guard ace_mon (this->mutex_,
00121                        this->cond_,
00122                        this->pending_writes_,
00123                        this->writing_,
00124                        this->collection_);
00125 
00126   ace_mon.copy->collection.disconnected (proxy ACE_ENV_ARG_PARAMETER);
00127 }
00128 
00129 template<class PROXY, class COLLECTION, class ITERATOR, ACE_SYNCH_DECL> void
00130 TAO_ESF_Copy_On_Write<PROXY,COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00131     shutdown (ACE_ENV_SINGLE_ARG_DECL)
00132 {
00133   // We need to perform a copy to follow the protocol.
00134   Write_Guard ace_mon (this->mutex_,
00135                        this->cond_,
00136                        this->pending_writes_,
00137                        this->writing_,
00138                        this->collection_);
00139 
00140   ace_mon.copy->collection.shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
00141 }
00142 
00143 template<class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00144 TAO_ESF_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00145     TAO_ESF_Copy_On_Write_Write_Guard (ACE_SYNCH_MUTEX_T &m,
00146                                        ACE_SYNCH_CONDITION_T &c,
00147                                        int &p,
00148                                        int &w,
00149                                        Collection*& cr)
00150       :  copy (0),
00151          mutex (m),
00152          cond (c),
00153          pending_writes (p),
00154          writing_flag (w),
00155          collection (cr)
00156 {
00157   {
00158     ACE_GUARD (ACE_SYNCH_MUTEX_T, ace_mon, this->mutex);
00159 
00160     this->pending_writes++;
00161 
00162     while (this->writing_flag != 0)
00163       this->cond.wait ();
00164 
00165     this->writing_flag = 1;
00166   }
00167 
00168   // Copy outside the mutex, because it may take a long time.
00169   // Nobody can change it, because it is protected by the
00170   // writing_flag.
00171 
00172   // First initialize it (with the correct reference count
00173   ACE_NEW (this->copy, Collection);
00174   // Copy the contents
00175   this->copy->collection = this->collection->collection;
00176 
00177   // Increase the reference counts
00178   ITERATOR end = this->copy->collection.end ();
00179   for (ITERATOR i = this->copy->collection.begin (); i != end; ++i)
00180     {
00181       (*i)->_incr_refcnt ();
00182     }
00183 }
00184 
00185 template<class COLLECTION, class ITERATOR, ACE_SYNCH_DECL>
00186 TAO_ESF_Copy_On_Write_Write_Guard<COLLECTION,ITERATOR,ACE_SYNCH_USE>::
00187     ~TAO_ESF_Copy_On_Write_Write_Guard (void)
00188 {
00189   Collection *tmp = 0;
00190   {
00191     ACE_GUARD (ACE_SYNCH_MUTEX_T, ace_mon, this->mutex);
00192 
00193     tmp = this->collection;
00194     this->collection = this->copy;
00195     this->writing_flag = 0;
00196     this->pending_writes--;
00197 
00198     this->cond.signal ();
00199   }
00200   // Delete outside the mutex, because it may take a long time.
00201   tmp->_decr_refcnt ();
00202 }
00203 
00204 // ****************************************************************
00205 
00206 TAO_END_VERSIONED_NAMESPACE_DECL
00207 
00208 #endif /* TAO_ESF_COPY_ON_WRITE_CPP */

Generated on Thu Nov 9 13:08:13 2006 for TAO_ESF by doxygen 1.3.6